New to coding and i'm building a text-based game. Not sure what my issue is

Question:

The game works fine, except im trying to add a restart function where the game restarts when a user inputs restart into any input. Here is my code, not sure what the issue is.

# Scoring Elements #
incorrect = 0

# Key Variables #
reset = True
tutorial_input = ""
input1 = ""
input2 = ""
input3 = ""
input4a = ""
input4b = ""
input5 = ""
input6a = ""
input6b = ""
input6c = ""
input7a = ""
input7b = ""
input8wel = ""
input8a = ""
input8b = ""
input8c = ""
input9 = ""

def reset_check():
    global tutorial_input, input1, input2, input3, input4a, input4b, input5, input6a, input6b, input6c, input7a, input7b, input8wel, input8a, input8b, input8c, input9, reset
    if (
        tutorial_input.lower() == "restart" or
        input1.lower() == "restart" or
        input2.lower() == "restart" or
        input3.lower() == "restart" or
        input4a.lower() == "restart" or
        input4b.lower() == "restart" or
        input5.lower() == "restart" or
        input6a.lower() == "restart" or
        input6b.lower() == "restart" or
        input6c.lower() == "restart" or
        input7a.lower() == "restart" or
        input7b.lower() == "restart" or
        input8wel.lower() == "restart" or
        input8a.lower() == "restart" or
        input8b.lower() == "restart" or
        input8c.lower() == "restart" or
        input9.lower() == "restart"
    ):
        reset = True

# Welcome #
while reset:
    reset = False
    print("Welcome to the Every Dew Challenge. Solve each puzzle to pass the Guardians")
    name = input("What is your name? ")
    print("Welcome aboard " +name+", good luck!")
    reset = 1

    # Restart Function #
    print(''' 
If at anytime during the game you want to restart, input RESTART and the game will restart
''')

    # Tutorial #
    print("Be silent to continue.")
    tutorial_input = input("")
    reset_check()

    while tutorial_input != "" :
        print("Shh....")
        incorrect += 1
        tutorial_input = input("")
        reset_check()

    else :
        print("You may pass")

    # level 1 #

    print('''
    ----- Level 1 -----
     ''')


    print("I'm the Guardian, say my name to pass")
    input1 = input("")
    reset_check()

    while input1 != "my name" :
        print("Say my name to pass")
        incorrect += 1
        input1 = input("")
        reset_check()

    else :
        print("You may pass")

    # Level 2 #
    print('''
    ----- Level 2 -----
     ''')

    print("Two digits are needed - first and last.")
    input2 = input("")
    reset_check()

    while input2 != "23" :
        if input2 == "first" :
            print("a couple...")
            input2 = input("")
            reset_check()

        elif input2 == "last":
            print("this many is a crowd...")
            input2 = input("")
            reset_check()

        else :
            print("first and last")
            incorrect += 1
            input2 = input("")
            reset_check()

    else :
        print("You may pass")

    # Level 3 #
    print('''
    ----- Level 3 -----
     ''')

    print("shout the answer to pass.")
    input3 = input("")
    reset_check()

    while input3 != "THE ANSWER":
        print("You have to SHOUT the answer.")
        incorrect += 1
        input3 = input("")
        reset_check()

    else :
        print("You may pass")

    # Level 4 #
    print('''
    ----- Level 4 -----
     ''')

    verdict = 0
    print("Knock knock...")
    input4a = input("")
    reset_check()


    while verdict == 0 :
        if input4a == "who's there" :
                print("You!")
                input4b = input("")
                reset_check()

                if input4b == name :
                    print("You may pass.")
                    verdict = 1

                elif input4b != name :
                    print("Knock knock...")
                    incorrect += 1
                    input4a = input("")
                    reset_check()

        elif input4a != "who's there":
            print("Knock knock...")
            incorrect += 1
            input4a = input("")
            reset_check()

    # Level 5 #
    print('''
    ----- Level 5 -----
     ''')
    print("Say something 3 times in a row.")
    counter = 0
    input5 = input("")
    reset_check()

    while counter < 3 :

        if input5 != "something" :
            counter = 0
            print("Say something 3 times in a row.")
            incorrect += 1
            input5 = input("")
            reset_check()

        elif input5 == "something" :
            counter = counter + 1

            if counter == 3:
                print("You may pass")

            else:
                input5 = input("")
                reset_check()

    # Level 6 #
    print('''
    ----- Level 6 -----
     ''')

    passed = "false"
    print("What ends everyone?")
    input6a = input("")
    reset_check()


    while passed == "false":
        if input6a == "death" :
            print("...and that links to?")
            input6b = input("")
            reset_check()

            while input6b != "honey" :
                incorrect += 1
                print("death links to...")
                input6b = input("")
                reset_check()

            else:
                print("you may pass")
                passed = "true"

        elif input6a == "e" :
            print("...and that links to?")
            input6c = input("")
            reset_check()

            while input6c != "elephant" :
                print("e links to...")
                incorrect += 1
                input6c = input("")
                reset_check()

            else :
                print("you may pass")
                passed = "true"

        else :
            print("What ends everyone?")
            incorrect += 1
            input6a = input("")
            reset_check()



    # Level 7 #
    print(
        """
    ----- Level 7 -----
     """
    )

    passed = "false"
    print("Are you happy or sad?")
    input7a = input("")
    reset_check()

    while passed == "false":
        if input7a == "happy":
            print("0b00011111011000000000")
            input7b = input("")
            reset_check()

            while input7b != ":)":
                print("Show me your smile")
                incorrect += 1
                input7b = input("")
                reset_check()
            else:
                print(chr(0b00011111011000000000))
                passed = "true"
                print("You may pass")

        elif input7a == "sad":
            print("0b00011111011000010100")
            input7c = input("")
            reset_check()

            while input7c != ":(":
                print("Show me your frown")
                incorrect += 1
                input7c = input("")
                reset_check()
            else:
                print(chr(0b00011111011000010100))
                passed = "true"
                print("You may pass")
        else:
            print("Are you happy or sad?")
            incorrect += 1
            input7a = input("")
            reset_check()



    #Level 8 - My First Custom Level #
    print('''
    ----- Level 8 -----
    ''')

    lives = 3
    level = 1



    print("Finish the lyrics to these 3 famous songs, get them wrong and face the consequences.")
    print(str(lives) + " Lives remaining.")
    input8wel = input(" -- Press Enter to begin -- ")
    reset_check()



    def check():
        global lives, level, incorrect
        if lives <= 0:
            print("You Have Run Out of Lives!")
            lives = 3
            level = 1
            incorrect += 1
            input8wel = input(" -- Press Enter to try again -- ")
            reset_check()


    while True:

        if level == 1:
            print("Level 1 - Happy birthday to ___ ")
            input8a = input("")
            reset_check()
            
            if input8a.lower() != "you":
                print("Wrong! Lose a life")
                lives -= 1
                check()
                print(str(lives) + " lives remaining.")

            else:
                print("Correct! On to Level 2")
                level = 2


        elif level == 2:
            print("Level 2 - To Advance Australia ____ ")
            input8b = input("")
            reset_check()
            
            if input8b.lower() != "fair":
                print("Wrong! Lose a life")
                lives -= 1
                check()
                print(str(lives) + " lives remaining.")

            else:
                print("Correct! On to Level 3")
                level = 3

        elif level == 3:
            print("Level 3 - God save the ____ ")
            input8c = input("")
            reset_check()
            
            if input8c.lower() != "king":
                print("Wrong! Lose a life")
                lives -= 1
                check()
                print(str(lives) + " lives remaining.")

            else:
                break

    print("Correct! You may pass")


    # Level 9 - My Second Custom Level #

    print('''
    ----- Level 9 -----
     ''')

    print('''This is the final level and will require some creative thinking.
    ''')
    print('''The clues for this level are as follows:
        1. Do not input the same answer twice
        2. Only one answer is correct
        3. Go make your attempts quickly. Time is running out!

    The answer is an animal.''')

    input9 = input("What am I? ")
    reset_check()

    while input9.lower() != "dog":
        print("LOOK at the clues. The answer is staring at you!")
        incorrect += 1
        input9 = input("")
        reset_check()

    else :
        print("You have finished the game! Congratulations!")
        print("In total, you made "+str(incorrect)+" errors. That's not too bad!")

I’ve tried using ChatGPT but not much help. Any help would be appreciated, just not sure what my issue is here. To me it seems correct but the reset_check() function doesn’t work.

Asked By: angus Undies

||

Answers:

Your while reset: line is not evaluated at each call (or return) of reset_check(), which is why your program is not resetting. You will need to determine some way to break out of the loop and re-evaluate, or a different method of iterating through your many challenges, such that the while reset: condition is evaluated (and acted upon if required) each time.

Answered By: Stitt

The problem is while you are changing the value of reset, the pointer is already on the inner while loop so it’s not checking for the outer while reset loop weather reset is true or false. Instead of this you show wrap the main code into a function itself and call for the reset function and when it returns true you should return out of the main function thus making the whole main function stop. The code should look like this:

def reset_check():

  global tutorial_input, input1, input2, input3, input4a, input4b, input5, input6a, input6b, input6c, input7a, input7b, input8wel, input8a, input8b, input8c, input9, reset
  if (
    tutorial_input.lower() == "restart"
    or input1.lower() == "restart"
    or input2.lower() == "restart"
    or input3.lower() == "restart"
    or input4a.lower() == "restart"
    or input4b.lower() == "restart"
    or input5.lower() == "restart"
    or input6a.lower() == "restart"
    or input6b.lower() == "restart"
    or input6c.lower() == "restart"
    or input7a.lower() == "restart"
    or input7b.lower() == "restart"
    or input8wel.lower() == "restart"
    or input8a.lower() == "restart"
    or input8b.lower() == "restart"
    or input8c.lower() == "restart"
    or input9.lower() == "restart"
):
    return True

def main():

  global tutorial_input, input1, input2, input3, input4a, input4b, input5, input6a, input6b, input6c, input7a, input7b, input8wel, input8a, input8b, input8c, input9, reset, incorrect
  while reset:
    reset = False
    print(
        "Welcome to the Every Dew Challenge. Solve each puzzle to pass the Guardians"
    )
    name = input("What is your name? ")
    print("Welcome aboard " + name + ", good luck!")
    reset = 1

    # Restart Function #
    print(
        """ 
If at anytime during the game you want to restart, input RESTART and the game will restart
"""
    )

    # Tutorial #
    print("Be silent to continue.")
    tutorial_input = input("")
    if reset_check():
        return

main()

The rest of the code is on you!

Answered By: AhmadFaraz
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.