def purchase(amount, day, month, country):
global history
history += [0, 0, days_in_months(month - 1) + day]
if history[len(history) - 1] <= history[len(history)]:
return "Successful purchase"
else:
return "error"
当我尝试运行此程序时,我收到消息“列表索引超出范围” 我该如何解决这个问题?
最佳答案
因为 history[len(history)]
的访问超出了数组的限制,例如,如果您有一个包含 3 个元素的数组,则最后一个元素将为 history[2]
而 len(history)
将为 3,这超出了限制。 Python 中的数组从 0 开始计数。
关于Python列表: index out of range?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33058778/