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

FOR PYTHON: Write a function ticker () that takes a string representing the name

ID: 643949 • Letter: F

Question

FOR PYTHON: Write a function ticker() that takes a string representing the name of a file as a parameter. The file contains company names and stock (ticker) symbols. In the file, the company name appears on one line and the stock symbol for that company appears on the following line. Both the company name and the stock symbol appear in all caps. For example, see the file nasdaq.txt in the zip file containing the assignment template. The ticker() function reads the file and stores the company names and stock symbols in a dictionary. It then provides an interface to the user so that he/she can obtain the stock symbol for a given company. The user should be able to provide the name of the company in lowercase, uppercase, or mixed case and get the stock symbol associated with the company found in the file the function processed. When the user enters a blank company name, the program should stop. If the file provided as a parameter cannot be opened, the function should dispaly an appropriate message and stop. Test your program on the nasdaq.txt file provided with the assignment template. Some sample interactions are provided below:

FOR PYTHON: Write a function ticker() that takes a string representing the name of a file as a parameter. The file contains company names and stock (ticker) symbols. In the file, the company name appears on one line and the stock symbol for that company appears on the following line. Both the company name and the stock symbol appear in all caps. For example, see the file nasdaq.txt in the zip file containing the assignment template. The ticker() function reads the file and stores the company names and stock symbols in a dictionary. It then provides an interface to the user so that he/she can obtain the stock symbol for a given company. The user should be able to provide the name of the company in lowercase, uppercase, or mixed case and get the stock symbol associated with the company found in the file the function processed. When the user enters a blank company name, the program should stop. If the file provided as a parameter cannot be opened, the function should dispaly an appropriate message and stop. Test your program on the nasdaq.txt file provided with the assignment template. Some sample interactions are provided below:

Explanation / Answer

def ticker(s):
   d = {}
   with open(s) as f:
       for line1 in f:
           line1 = line1.strip()
           line1 = line1.upper()
           line2 = f.readline().strip()
           line2 = line2.upper()
           d[line1] = line2
   while True:
       t = input('Enter Company name: ')
       if t == '':
           print('Thank you for using the ticker lookup system!')
           return
       t = t.upper()
       if t not in d:
           print(t + ' is not a company in the ticker system')
       else:
           print('ticker symbol: ' + d[t])

def main():
   ticker('file.txt')

main()