我最近一直在测试我的程序,每次测试时,我都会收到相同的错误消息: IndexError:列表索引超出范围 该代码是
def NewOffer(CurrentOffer, MaximumPrice, y, NoOfBuyers, Buyers):
x = random.randint(0, NoOfBuyers)
SelectedBuyer = Buyers[x]
if CurrentOffer < MaximumPrice[x]:
CurrentOffer = CurrentOffer + random.randint(1, 500)
print(str(SelectedBuyer) + " renewed their offer and are now willing to pay £" +
str(CurrentOffer) + " for " + str(AuctionedItem))
x = random.randint(0, NoOfBuyers)
NewOffer(CurrentOffer, MaximumPrice, y, NoOfBuyers, Buyers)
time.sleep(3)
唯一一次跳闸的线路是'''SelectedBuyer = Buyers [x]'''。我该如何解决?
似乎“买家”列表中的位置x没有元素,您可以通过以下方式检查该元素
要么
To generate a random number that in the length of the Buyers list. You may need to do the same for
MaximumPrice