0

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...")
Rowheat
  • 11
  • 3
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues). – wwii Sep 04 '22 at 17:53
  • 1
    Don't use recursive calls to re-start your program. Instead, use a `while` with `break` statements or a flag (e.g. some variable taking a boolean). Take a look at [this](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) and [this](https://stackoverflow.com/questions/189645/how-can-i-break-out-of-multiple-loops). – Ignatius Reilly Sep 04 '22 at 18:17
  • BTW, you may have forgotten `array = []` at the beginning of the function definition. Or you had it defined in the main scope, which is not a good idea. – Ignatius Reilly Sep 04 '22 at 18:22

1 Answers1

0

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.

ss3387
  • 299
  • 3
  • 19