Home > Net >  How can I stop the loop?
How can I stop the loop?

Time:09-06

When it is asking to play again for 1st time and I enter x, the program ends. But if I continue playing and during the 2nd time, when I enter x, it does not end. Why?

def b():
    random_number=random.randint(1, 10)
    turn=0
    score=100
    while True:
        try :
            num=input("Enter a number between 1 and 10 : ")
            array.append(num)
            turn =1
            if int(num)>10 or int(num)<1:
                print("Please enter a number within valid range.")
            if 1<array.count(num):
                print("You have entered this number %s time/s"%array.count(num))
            if int(num)==random_number:
                print("Excellent you found the number in %s turn/s"%turn)
                print("Your score is " str(score))
                choice=input("Press enter key to play again or enter x to quit : ")
                if choice.lower()=="x":
                    print("Nice to meet you")
                    break  
                else:
                    b()
                    score=100
                    turn=0
            elif int(num)>random_number and int(num)>0 and int(num)<11:
                print("Go lower")
                score-=10
            elif int(num)<random_number and int(num)>0 and int(num)<11:
                print("Go upper")
                score-=10
        except ValueError as err:
            print("Oh no!, that is not a valid value. Try again...")

CodePudding user response:

You have to add break in the else statement which is below if choice.lower()=="x". This is because the when you run the function b it will start another while loop so there are 2 while loops running. You are only breaking when the user wants to close but according to your structure of code you should also break when he says he wants to play again because a new while loop will start anyway. This is the code:

def b():
    random_number=random.randint(1, 10)
    turn=0
    score=100
    while True:
        try :
            num=input("Enter a number between 1 and 10 : ")
            array.append(num)
            turn =1
            if int(num)>10 or int(num)<1:
                print("Please enter a number within valid range.")
            if 1<array.count(num):
                print("You have entered this number %s time/s"%array.count(num))
            if int(num)==random_number:
                print("Excellent you found the number in %s turn/s"%turn)
                print("Your score is " str(score))
                choice=input("Press enter key to play again or enter x to quit : ")
                if choice.lower()=="x":
                    print("Nice to meet you")
                    break  
                else:
                    b()
                    score=100
                    turn=0
                    break
            elif int(num)>random_number and int(num)>0 and int(num)<11:
                print("Go lower")
                score-=10
            elif int(num)<random_number and int(num)>0 and int(num)<11:
                print("Go upper")
                score-=10
        except ValueError as err:
            print("Oh no!, that is not a valid value. Try again...")

If you think this answers your question, you can tick it.

  • Related