USING PYTHON: Write function ticker() that takes a string (the name of a le) as
ID: 3763428 • Letter: U
Question
USING PYTHON: Write function ticker() that takes a string (the name of a le) as input. The le will contain company names and stock (ticker) symbols. In this le, 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 le and store the name and stock symbol in a dictionary. Then it will provide an interface to the user so that the user can obtain the stock symbol for a given company. Test your code on the NASDAQ 100list of stock given in le nasdaq.txt
>>> ticker( ' nasdaq.txt ' )
Enter Company name: YAHOO
Ticker symbol: YHOO
Enter Company name: GOOGLE INC
Ticker symbol: GOOG
…
how file (nasdaq.txt) looks once opened:
ACTIVISION INC
ATVI
ADOBE SYS INC
ADBE
ALTERA CORP
ALTR
AMAZON
AMZN
AMERICAN POWER CONVER CORP
APCC
AMGEN
AMGN
APOLLO GROUP-A
APOL
APPLE COMPUTER
AAPL
APPLIED MATERIAL
AMAT
ATI TECHNOLOGIES
ATYT
AUTODESK
ADSK
BEA SYSTEMS
BEAS
BED BATH &BEYOND
BBBY
BIOGEN
BIIB
BIOMET
BMET
...
WHOLE FOODS MARKET
WFMI
WYNN RESORTS, LTD
WYNN
XILINX
XLNX
XM SATELLITE RADIO HOLDINGS
XMSR
YAHOO
YHOO
Explanation / Answer
def ticker(filename):
ticker_dict={}
# Open a file
fo = open(filename, "r")
line = fo.readline()
line1 = line
line = fo.readline()
line2 = line
ticker_dict[line1.rstrip()] = line2.rstrip(); # Add new entry
while line:
line = fo.readline()
line1 = line
line = fo.readline()
line2 = line
ticker_dict[line1.rstrip()] = line2.rstrip(); # Add new entry
name=input("Enter Company name: ")
print ("Ticker symbol: ",ticker_dict[name])
# Close opend file
fo.close()
ticker('nasdaq.txt')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.