PYTHON CODES STARTING WITH def: 1.you are writing a program to analyze stock mar
ID: 3832000 • Letter: P
Question
PYTHON CODES STARTING WITH def:
1.you are writing a program to analyze stock market prices. the prices are stored "stockprices.txt". each line contains 3 items seperated by commas: the date, the stock name, and the closing
3/10/2013,GOOG,544.40
3/12/2013,YHOO, 51.75
11/28/2013, TWTR, 41.85
WRITE A FUNCTION CALLED firststockname which takes as input the filename to open and prints the name of the first stock listed.
ex. funtion call
firststockname("stockprices.txt") # should print out GOOG if Google were the first company listed in the file.
Explanation / Answer
def firststockname(file_name):
try:
file = open(file_name,"r")
try:
print file.readline().split(',')[1]
except:
print "Error in text format OR empty text file"
except:
print "Error in file name OR file does not exist"
firststockname("stock.txt")
#give the file name which the stock details are given.
#if it is in another folder give relative path like stock/stock.txt
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.