Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need in python, thank you 6.21 Write function ticker that takes a string (the na

ID: 3923749 • Letter: N

Question


Need in python, thank you 6.21 Write function ticker that takes a string (the name of a file) as input. The file will contain company names and stock (ticker) symbols. In this file, a company name will occupy a line and its stock symbol will be in the next line. Following this line will be a line with another company name, and so on. Your program will read the file and store the name and stock symbol in a dictionary. Then it will provide an interface to the user so the user can obtain the stock symbol for a given company. Test your code on the NASDAQ 100 list of stock given in file nasdaq.txt >> ticker ('nasdaq. txt') Enter Company name: YAHOO Ticker symbol: YHOO Enter Company name: GOOGLE INC Ticker symbol: G00G ('nasdaq.txt')

Explanation / Answer

import os

def ticker(filename):

   key = [] # to store company name
   value = [] # to store ticker symbol

   with open(filename) as f:
       lst = [line.rstrip() for line in f]

   for line in range(0, len(lst), 2):
      
       key.append(lst[line])

   for line in range(1, len(lst), 2):
            value.append(lst[line])

   dictionary = dict(zip(key, value))
  
   user_input = raw_input("Please enter a company name: ")
if user_input in dictionary:
return dictionary[user_input]
else:
return "Not found"
  
filename = raw_input('Enter a filename: ')
message = ticker(filename)
print "Ticket Symbol :", message

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote