pythonadventure

python adventure game name error after choice


I'm trying to program a template for an adventure game. I'm new to python and watched a few videos to help me out. I used a lot of code from there. Everything is running as I wish except the def for the choice. I want the correct answer to be random. However, after putting in my answer, I get a name error:

    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

In the videos it worked this way. I'm confused as what to define ´choice´ or the chosenPath... I tried a few things, but it never worked. Please help me out.

Here is the code:

    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?")**

Ive cut a few strings and did not include the intro here since the error is the game loop and the choosePath and checkPath.

Thank you all so much for your help. Lots of love, Amber


Solution

  • You haven't defined choice, the computer doesn't know what it means. You could try to do choice = choosePath() before executing checkPath(choice)