选择python冒险游戏名称后出现错误



我正在尝试为一个冒险游戏编程一个模板。我是python的新手,看了一些视频来帮助我。我用了很多代码。除了选择的def之外,一切都在按我的意愿运行。我希望正确的答案是随机的。然而,在输入答案后,我得到了一个名称错误:

NameError                                 Traceback (most recent call last)
Input In [1], in <cell line: 70>()
71 intro ()
72 choosePath()
---> 73 checkPath(choice)
74 playAgain = input("Möchtest du erneut spielen? ja oder nein?")
NameError: name 'choice' is not defined

在视频中,它是这样工作的。我很困惑该如何定义"选择"或选择的道路。。。我试了几件事,但都没用。请帮帮我。

这是代码:

def choosePath():
path = "" 
while path != "1" and path != "2": 
path = input ("Where do you wanna go? for left put 1 for right put 2:")

return (path) 
def checkPath(chosenPath):
correctPath = random.randint(1, 2)
if chosenPath == str(correctPath):
print ("Du hast es geschafft.")
else:
print ("Der Tunnel stürzt ein und umhüllt dich in einen brutalen Schlaf.")
time.sleep (3)
print ()
print ("Warme Dunkelheit.")
time.sleep (1)
print ("Du atmest ein letztes Mal aus.")
#gameloop
playAgain = "ja"
while playAgain == "ja":
intro ()
choosePath()
checkPath(choice)
playAgain = input("Möchtest du erneut spielen? ja oder nein?")**

我剪了一些字符串,没有在这里包括介绍,因为错误是游戏循环和choosePath和checkPath。

非常感谢大家的帮助。充满爱,琥珀色

您还没有定义选择,计算机不知道它的含义。您可以尝试在执行checkPath(choice)之前执行choice = choosePath()

最新更新