python - Why getting invalid literal when running this script? -
when run following script, valueerror: invalid literal int() base 10: '2-' when date contains 1 digit (ie 02-02-2011). works fine when date has 2 digits (ie 11-11-2011). reason this, , how can fix it?
from __future__ import division easygui import * import ystockquote import datetime import math def main(): stock = 'nflx' name = ystockquote.get_company_name(stock) start_date = create_date(02,13,2009) end_date = create_date(10,21,2014) start_price = get_price_on_date(stock,start_date) end_price = get_price_on_date(stock,end_date) if not isinstance(start_price,str): print "please enter different start date, market closed on day chose!" quit() else: start_price = float(start_price) if not isinstance(end_price,str): print "please enter different end date, market closed on day chose!" quit() else: end_price = float(end_price) no_of_shares = math.floor(10000/float(start_price)) profit = (end_price-start_price)*no_of_shares print "the profit resulting investing $10,000 in " + name + " " + start_date + " " + end_date + " have been " + "$" + str(profit) + " or return of {:.2%}".format(profit/10000) + " ." def get_price_on_date(stock,date): = ystockquote.get_historical_prices(stock,date,date) key, value in a.iteritems() : key, value in value.iteritems() : if key == 'close': return value def create_date(month,day,year): date_string = str(year) + "-" + str(month) + "-" + str(day) return date_string if __name__ == '__main__': main()
your create_date()
function not returning two-digit day , month numbers in cases.
try formatting values explicitly instead of using str()
:
date_string = "{0:04}-{1:02}-{2:02}".format(year, month, day)
Comments
Post a Comment