I need a program function() that takes a string which represents the name of a f
ID: 3644247 • Letter: I
Question
I need a program function() that takes a string which represents the name of a file as a parameter. In this file, it contains company names and stock symbols. Also, the company name appears on one line and the stock symbol for that company is on the next line. They appear in all caps. The function() program reads the file and then it will store the name of the company and the stock symbols in a DICTIONARY. Then it gives an interface to the user so that the user can get the stock symbol for the given company. It should be ok in lowercase, uppercase, mixed and still be able to get the symbol and when a blank company name is entered, the program should end.
- This is how it looks in the word file.
ACTIVISION INC
ATVI
ADOBE SYS INC
ADBE
ALTERA CORP
ALTR
AMAZON
AMZN
With many more also.
Example of how it runs
Explanation / Answer
def function(filename):
f=open(filename)
names=[]
codes=[]
linenumber=0
#First read in the file
for line in f:
linenumber=linenumber+1
if linenumber%2:
names.append(line.lower()) #lower the case so case doesn't matter
else:
codes.append(line)
f.close()
# Now make the dictionary
dic={}
for i in range(len(names)):
dic[names[i]]=codes[i]
done = False
while not done:
cname=raw_input("Enter a company name:")
if cname="":
print "Thank you for using the ticker lookup system"
done = True
continue
cname = cname.lower() # So case doesn't matter
if cname in names:
print "Ticker symbol:", dic[cname]
else:
print "No such company."
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.