In PYTHON 3, (please) write function ticker() that takes a string (the name of a
ID: 3922273 • Letter: I
Question
In PYTHON 3, (please) 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.
nasdaq.txt contents:
Explanation / Answer
import os
import sys
def ticker():
try:
filename = raw_input("Please enter a filename: ")
txt = open(filename)
list1 = []
dict1 = {}
for line in txt:
line = line.split(' ')
list1.append(line[0])
j = 1
for i in range(0,len(list1),2):
dict1[list1[i]] = list1[j]
j = j+2
while 1:
try:
comp = raw_input("Input the company name: ")
print dict1[comp]
except:
print "Company name not found!!!"
break
return
except:
print "File not found!!!"
ticker()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.