[Python] Quitting to Game Over when player dies

Discussão em 'Python' iniciado por Stack, Agosto 4, 2025.

  1. Stack

    Stack Membro Participativo

    So, I'm working on a python text-based game that I can share with my friends. I've got most of the game working, but I'm struggling with the game over portion for when a user selects certain commands. I'm not using pygame for this because I can't find a 64-bit version. Below is what I'm looking at. What do I put in the gameOver function to actually quit the game, or if the player wants, try again?

    import time
    import random
    import sys

    def searchAreas():
    print("1. Area1")
    print("2. Area2")
    print("3. Area3")
    print("4. Just give up")

    def badResponse():
    print("Sorry, I don't understand " + search_area)

    def gameOver():
    print("You decide to give up because life is too hard.")
    print("Would you like to try again?")
    player_choice = input("> ")
    if player_choice == "Yes":
    mainGame()
    elif player_choice == "No":
    print("# what goes here to quit the game?")
    else:
    badResponse()

    def mainGame():
    search_area_options = ["1","2","3"]
    search_area = ""
    while search_area not in search_area_options:
    print("Where do you want to start looking?")
    searchAreas()
    search_area = str(input("> "))
    if search_area == "1":
    print("Text here")
    elif search_area == "2":
    print("Text here")
    elif search_area == "3":
    print("text here")
    elif search_area == "4":
    gameOver()
    else:
    badResponse()

    mainGame()



    When typing anything but the four options, or when going into the gameOver function, I'm seeing this error:

    Traceback (most recent call last):
    File "./test.py", line 45, in <module>
    mainGame()
    File "./test.py", line 43, in mainGame
    badResponse()
    File "./test.py", line 14, in badResponse
    print("Sorry, I don't understand " + search_area)
    NameError: name 'search_area' is not defined

    Continue reading...

Compartilhe esta Página