Python 3“ IndexError:弹出索引超出范围”

问题描述

我被要求为我的计算机科学作业模拟蒙蒂厅问题。一切顺利,直到我得到他的错误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,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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)