Home > Blockchain >  How do I create a function that will end the program?
How do I create a function that will end the program?

Time:12-03

I have difficulties creating a counter (which is errorCount) for my while loop statement. I want my counter to function so that if the user answered a question incorrectly 5 times the program will terminate. furthermore, I have 3 questions for the user and I want to accumulate all the errorCounts so that if it hit 5 the program will terminate.

for example: if the user answers question 1 incorrectly twice then the errorCount will be two. If the user answers question 2 incorrectly three times then the program will be terminated. However, the program is allowing the user to make 5 mistakes for every problem.

# Level 5:
print("You have the jewel in your possession, and defeated Joker at his own game")
print("You now hold the precious jewel in your hands, but it's not over, you must leave the maze!")
print("*You must now choose 'Right', 'Left', or 'Straight' as you exit the maze. Keep trying until you find your path.*")

# put an error limit
# space everything out to make it look more neat
# Make sure you can fail the level

**errorCount = 0**

position = 0
while True:
    answer1 = input("Choose either Right, Left, Straight: ")
    
    
    try:
        if answer1.lower() == "right":
            print("You have chosen the correct path, now you proceed to the next step!")
            print()
            break
   *     
        if errorCount == 5:
           print("you made too many mistakes and got captured, you have to restart")*
           
        elif answer1.lower() == "left":
            print("You see a boulder blocking your path which forces you to go back.")
            errorCount = 1   errorCount
            
        elif answer1.lower()  == "straight":
            print("On your way to the next stage you are exposed to a toxic gas that forces you to go back .")
            errorCount = 1   errorCount
            
        else:
            print("Wrong input. Please try again..")
            errorCount = 1   errorCount
    except Exception:
        print("Wrong input. Please try again..")

# if errors >= 5:

while True:
    answer1 = input("Choose either Right, Left, Straight: ")
    
    if errorCount == 5:
       print("you made too many mistakes and got captured, you have to restart")
       
    try:
        if answer1.lower() == "straight":
            print("You have chosen the correct path, now you proceed to the next step!")
            break
    
        
        elif answer1.lower() == "left":
            print("You chose the wrong path, go back")
            errorCount = 1   errorCount
            
        elif answer1.lower() == "right":
            print("You chose the wrong path, go back")
            errorCount = 1   errorCount
            
        else:
            print("Wrong input. Please try again..")
            errorCount = 1   errorCount
            
    except Exception:
        print("Wrong input. Please try again..")

print("You are now on the third stage, you notice a screen that is asking you a riddle")
while True:
    riddle1 = input("What gets wet when drying? ")
    
    if errorCount == 5:
       print("you made too many mistakes and got captured, you have to restart")
       
    try:
        if riddle1.lower() == "towel":
            print("You have chosen the correct answer")
            print("The giant stone blocking the entrance of the maze opens, and the outside lights shine through..")
            break
        
        else:
            print("Incorrect! Try again..")
            errorCount = 1   errorCount
            print("Heres a hint: You use it after taking a shower...")
            
    except Exception:
        print("Incorrect! Try again..")
        errorCount = 1   errorCount







I do not know how to fix this issue 

CodePudding user response:

You have overly complicated the code.

questions = ["You have the jewel in your possession, and defeated Joker at his own game",
"You now hold the precious jewel in your hands, but it's not over, you must leave the maze!",
"*You must now choose 'Right', 'Left', or 'Straight' as you exit the maze. Keep trying until you find your path.*"]

error_count = 0

for id, query in enumerate(questions):

    if id == 0:
        # call method for query 1
        # write your while error_count < 5 loop inside the method
        # return error_count to check.
        pass
    elif id == 1:
        pass
    elif id == 2:
        pass
    
    if error_count > 5:
        break

You can also raise a User warning if you use try/except.

except UserWarning:
    if error_count > 5:
        print("you made too many mistakes and got captured, you have to restart")
        break

CodePudding user response:

You just forgot to put the errorCount conditional in the last loop, also it is better that you put this before the user input so that it doesn't ask the question 6 times instead of the 5 you want. Finally, it is necessary to add a break at the end of the conditional so that there is not an infinite loop

# Level 5:
print("You have the jewel in your possession, and defeated Joker at his own game")
print("You now hold the precious jewel in your hands, but it's not over, you must leave the maze!")
print("*You must now choose 'Right', 'Left', or 'Straight' as you exit the maze. Keep trying until you find your path.*")

# put an error limit
# space everything out to make it look more neat
# Make sure you can fail the level

errorCount = 0

position = 0
while True:
    if errorCount == 5:
        print("you made too many mistakes and got captured, you have to restart")
        break
    
    answer1 = input("Choose either Right, Left, Straight: ")
    
    try:
        if answer1.lower() == "right":
            print("You have chosen the correct path, now you proceed to the next step!")
            print()
            break
        
        elif answer1.lower() == "left":
            print("You see a boulder blocking your path which forces you to go back.")
            errorCount = 1   errorCount
        
        elif answer1.lower()  == "straight":
            print("On your way to the next stage you are exposed to a toxic gas that forces you to go back .")
            errorCount = 1   errorCount
        
        else:
            print("Wrong input. Please try again..")
            errorCount = 1   errorCount
    except Exception:
        print("Wrong input. Please try again..")

# if errors >= 5:

while True:
    if errorCount == 5:
        print("you made too many mistakes and got captured, you have to restart")
        break
    
    answer1 = input("Choose either Right, Left, Straight: ")
    
    try:
        if answer1.lower() == "straight":
            print("You have chosen the correct path, now you proceed to the next step!")
            break
        
        elif answer1.lower() == "left":
            print("You chose the wrong path, go back")
            errorCount = 1   errorCount
        
        elif answer1.lower() == "right":
            print("You chose the wrong path, go back")
            errorCount = 1   errorCount
        
        else:
            print("Wrong input. Please try again..")
            errorCount = 1   errorCount
            
    except Exception:
        print("Wrong input. Please try again..")

print("You are now on the third stage, you notice a screen that is asking you a riddle")

while True:
    if errorCount == 5:
        print("you made too many mistakes and got captured, you have to restart")
        break
    
    riddle1 = input("What gets wet when drying? ")
    
    if errorCount == 5:
        print("you made too many mistakes and got captured, you have to restart")

    try:
        if riddle1.lower() == "towel":
            print("You have chosen the correct answer")
            print("The giant stone blocking the entrance of the maze opens, and the outside lights shine through..")
            break
        
        else:
            print("Incorrect! Try again..")
            errorCount = 1   errorCount
            print("Heres a hint: You use it after taking a shower...")
            
    except Exception:
        print("Incorrect! Try again..")
        errorCount = 1   errorCount

I recommend that instead of using:

errorcount = 1   errorcount

It is better to use:

errorcount  = 1
  • Related