gpt4 book ai didi

Python列表: index out of range?

转载 作者:行者123 更新时间:2023-12-01 04:26:32 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com