为什么在 运行 这个脚本时得到无效文字?
Why getting invalid literal when running this script?
当我 运行 以下脚本时,我得到 ValueError: invalid literal for int() with base 10: '2-' when the date only contains 1 digit (ie 02-02-2011).但是,当日期有 2 位数字(即 11-11-2011)时,它工作正常。这是什么原因,我该如何解决?
from __future__ import division
from 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 a different start date, the market was closed on the day you chose!"
quit()
else:
start_price = float(start_price)
if not isinstance(end_price,str):
print "Please enter a different end date, the market was closed on the day you 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 from investing ,000 in " + name + " from " + start_date + " to " + end_date + " would have been " + "$" + str(profit) + " or a return of {:.2%}".format(profit/10000) + " ."
def get_price_on_date(stock,date):
a = ystockquote.get_historical_prices(stock,date,date)
for key, value in a.iteritems() :
for 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()
您的 create_date()
函数并非在所有情况下都返回两位数的日期和月份。
尝试明确格式化值而不是使用 str()
:
date_string = "{0:04}-{1:02}-{2:02}".format(year, month, day)
当我 运行 以下脚本时,我得到 ValueError: invalid literal for int() with base 10: '2-' when the date only contains 1 digit (ie 02-02-2011).但是,当日期有 2 位数字(即 11-11-2011)时,它工作正常。这是什么原因,我该如何解决?
from __future__ import division
from 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 a different start date, the market was closed on the day you chose!"
quit()
else:
start_price = float(start_price)
if not isinstance(end_price,str):
print "Please enter a different end date, the market was closed on the day you 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 from investing ,000 in " + name + " from " + start_date + " to " + end_date + " would have been " + "$" + str(profit) + " or a return of {:.2%}".format(profit/10000) + " ."
def get_price_on_date(stock,date):
a = ystockquote.get_historical_prices(stock,date,date)
for key, value in a.iteritems() :
for 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()
您的 create_date()
函数并非在所有情况下都返回两位数的日期和月份。
尝试明确格式化值而不是使用 str()
:
date_string = "{0:04}-{1:02}-{2:02}".format(year, month, day)