我知道其他类似的问题也已提交到此站点,但是这些问题的答案都没有帮助我。
我正在尝试复制“蒙蒂大厅”问题,该问题在我做出更改选择之前一直有效。
def otherChoice (door,choice):
check = door + choice
if check == 3:
x=choice
print (x)
while x == choice or x == 3:
x=random.randint(1,3)
if check == 4:
x=choice
print(x)
while x == choice or x == 2:
x=random.randint(1,3)
if check == 5:
x=choice
print(x)
while x == choice or x == 3:
x=random.randint(1,3)
print(x)
choice=x
if choice==door:
x=1
return x
尝试运行此函数时,Python返回错误
Traceback (most recent call last):
File "C:/Users/Tristan/Documents/Monty.py", line 46, in <module>
changeCorrect = changeCorrect + otherChoice(trueDoor,playerChoice)
File "C:/Users/Tristan/Documents/Monty.py", line 29, in otherChoice
print(x)
UnboundLocalError: local variable 'x' referenced before assignment
如您所见,我为变量x分配了一个随机数,它是两个数字的组合之一。
因此,我不知道如何得到此错误。