Need in python thank you 621 Write function ticker that take
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\') Solution
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
