Python 3 "IndexError: pop index out of range"
Python 3 "IndexError: pop index out of range"
我被要求为我的计算机科学作业模拟蒙蒂霍尔问题。一切进展顺利,直到我收到错误 IndexError: pop index out of range
。我知道我的代码对于你们所有真正的程序员来说可能相当草率和可怕,所以如果你们很难理解这一点,我很抱歉。我只是在寻找索引错误的解决方案,请尽量不要让它们太复杂——这是我在离开学校 6 个月后第一次使用 python。非常感谢任何帮助。
import random
import time
wins_switch = 0
losses_switch = 0
total_switch = 0
wins_stick = 0
losses_stick = 0
total_stick = 0
#while True :
print ("Monty hall problem model")
global doors
doors = [0, 0, 1]
which = random.randint(0,2)
print ("This is the door that has been chosen: " +(str(which)))
stick_or_switch = random.randint(0,1)
if which == 0:
doors.pop(1)
print("Door 1 has been removed")
print (doors)
if stick_or_switch == 0:
print("The computer has decided to stick with door 0")
print("Therefore, that is a loss")
doors.pop(2)
losses_stick =+1
print(doors)
else:
print("The computer has decided to switch")
print("Therefore that is a win")
doors.pop(0)
wins_switch = +1
print(doors)
elif which == 1:
doors.pop(0)
print ("Door 0 has been removed")
print(doors)
if stick_or_switch == 0:
print("The computer has decided to stick with door 1")
print("Therefore, that is a loss")
doors.pop(2)
losses_stick =+1
print(doors)
else:
print("The computer has decided to switch to door ")
print("Therefore that is a win")
doors.pop(2)
wins_switch = +1
print(doors)
else:
random_door = random.randint(0,1)
doors.pop(random_door)
print ("Door " + str(random_door) + " has been removed")
if random_door == 0:
if stick_or_switch == 0:
print("The computer had decided to stick with door 2")
print("Therefore, that is a win")
doors.pop(1)
wins_stick=+1
print(doors)
else:
print("The computer has decided to switch")
print("Therefore that is a loss")
doors.pop(2)
losses_switch =+1
print(doors)
else:
if stick_or_switch == 0:
print("The computer has decided to stick with door 2 ")
print("Therefore, that is a win")
doors.pop(0)
wins_stick=+1
print(doors)
else:
print("The computer has decided to switch")
print("Therefore that is a loss")
doors.pop(2)
losses_switch =+1
print(doors)
print (doors)
看看错误发生的行号...会给你提示。
查看您的代码发生的事情(最有可能)是您“弹出”一扇门,因此您只剩下 2 扇门,然后我猜,您尝试弹出第三扇门(其索引为 2
,即你使用 .pop(2)
) 并且你得到这个错误,因为你只剩下两扇门,所以没有这样的第三扇门。
我被要求为我的计算机科学作业模拟蒙蒂霍尔问题。一切进展顺利,直到我收到错误 IndexError: pop index out of range
。我知道我的代码对于你们所有真正的程序员来说可能相当草率和可怕,所以如果你们很难理解这一点,我很抱歉。我只是在寻找索引错误的解决方案,请尽量不要让它们太复杂——这是我在离开学校 6 个月后第一次使用 python。非常感谢任何帮助。
import random
import time
wins_switch = 0
losses_switch = 0
total_switch = 0
wins_stick = 0
losses_stick = 0
total_stick = 0
#while True :
print ("Monty hall problem model")
global doors
doors = [0, 0, 1]
which = random.randint(0,2)
print ("This is the door that has been chosen: " +(str(which)))
stick_or_switch = random.randint(0,1)
if which == 0:
doors.pop(1)
print("Door 1 has been removed")
print (doors)
if stick_or_switch == 0:
print("The computer has decided to stick with door 0")
print("Therefore, that is a loss")
doors.pop(2)
losses_stick =+1
print(doors)
else:
print("The computer has decided to switch")
print("Therefore that is a win")
doors.pop(0)
wins_switch = +1
print(doors)
elif which == 1:
doors.pop(0)
print ("Door 0 has been removed")
print(doors)
if stick_or_switch == 0:
print("The computer has decided to stick with door 1")
print("Therefore, that is a loss")
doors.pop(2)
losses_stick =+1
print(doors)
else:
print("The computer has decided to switch to door ")
print("Therefore that is a win")
doors.pop(2)
wins_switch = +1
print(doors)
else:
random_door = random.randint(0,1)
doors.pop(random_door)
print ("Door " + str(random_door) + " has been removed")
if random_door == 0:
if stick_or_switch == 0:
print("The computer had decided to stick with door 2")
print("Therefore, that is a win")
doors.pop(1)
wins_stick=+1
print(doors)
else:
print("The computer has decided to switch")
print("Therefore that is a loss")
doors.pop(2)
losses_switch =+1
print(doors)
else:
if stick_or_switch == 0:
print("The computer has decided to stick with door 2 ")
print("Therefore, that is a win")
doors.pop(0)
wins_stick=+1
print(doors)
else:
print("The computer has decided to switch")
print("Therefore that is a loss")
doors.pop(2)
losses_switch =+1
print(doors)
print (doors)
看看错误发生的行号...会给你提示。
查看您的代码发生的事情(最有可能)是您“弹出”一扇门,因此您只剩下 2 扇门,然后我猜,您尝试弹出第三扇门(其索引为 2
,即你使用 .pop(2)
) 并且你得到这个错误,因为你只剩下两扇门,所以没有这样的第三扇门。