0

So, I am learning Python 3. I tried writing a script that asks you to guess a number from 1 to 20. Now I know that this is not the best way of doing it, but when I run the code it goes into an infinite loop. What causes that? What did I do wrong? The purpose of my question is to understand my mistake and the process that leads to that. Thank you guys.

# Defining the function inside which the verification happens
def guesst(secretNumber, inputNumber):
    numberOfGuesses = 0
    if inputNumber >= 1 and inputNumber <= 20:
        while inputNumber:
            if inputNumber > secretNumber:
                numberOfGuesses += 1
                print('Your guess is too high.')
            elif inputNumber < secretNumber:
                numberOfGuesses += 1
                print('Your guess is too low.')
            elif inputNumber == secretNumber:
                print('Your guess is correct, congratulations! You\'ve my number in ', numberOfGuesses, 'guesses.')
                break
    else:
        print('Please enter a number between 1 and 20')

# Defining the variables used by the function
secretNumber = 11
inputNumber = int(input('I\'m thinking of a number between 1 and 20, try to guess which one: '))

# Calling in the function
guesst(secretNumber, inputNumber)

# -------------------------------------------------------
# I just changed my code to this and it worked, thank you!
# -------------------------------------------------------

def guesstt(secretNumber):
    numberOfGuesses = 0
    while secretNumber:
        inputNumber = int(input('I\'m thinking of a number between 1 and 20, try to guess which one: '))
        if inputNumber >= 1 and inputNumber <= 20:
            if inputNumber > secretNumber:
                numberOfGuesses += 1
                print('Your guess is too high.')
            elif inputNumber < secretNumber:
                numberOfGuesses += 1
                print('Your guess is too low.')
            elif inputNumber == secretNumber:
                print('Your guess is correct, congratulations! You\'ve my number in ', numberOfGuesses, 'guesses.')
                break
        else:
            print('Please enter a number between 1 and 20')

secretNumber = 11
guesstt(secretNumber)
4
  • 4
    Loop rule: the terminating condition has to change or it lasts forever. Does inputNumber ever change inside the loop block? Commented Apr 11, 2020 at 20:38
  • 1
    And how did you change your code? What did you do that inputNumber changes? BTW, according to the naming conventions in the Style Guide for Python Code inputNumber should be written as input_number. (edit: seems the comment I was referring to was deleted) Commented Apr 11, 2020 at 20:47
  • Thank you for the insight, I just realised and changed my code to : def guesstt(secretNumber): numberOfGuesses = 0 while secretNumber: inputNumber = int(input('I\'m thinking of a number between 1 and 20, try to guess which one: ')) Commented Apr 11, 2020 at 20:49
  • @Matthias I just updated my question with the correct format, please refer to the code above, and thank you for looking into this. Commented Apr 11, 2020 at 20:53

4 Answers 4

1

Read these two lines:

    if inputNumber >= 1 and inputNumber <= 20:
        while inputNumber:

The while loop body doesn't change inputNumber. If it is in the range 1..10, or 12..20, your loop amounts to while True: and it will run forever. You probably want the if test on the inside of the loop, and you definitely want the value to change by the time you come back to evaluate the looping condition, for example by inputing a new value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer I replaced my code with the correct format, you can check it above.
1

The secretNumber does not change? Use numberOfGuesses in the if and elif

1 Comment

Thank you, yes, I just realised. I changed it: def guesstt(secretNumber): numberOfGuesses = 0 while secretNumber: inputNumber = int(input('I\'m thinking of a number between 1 and 20, try to guess which one: '))
0

The variable inputNumber isn't updated within the while loop.

Try adding:

inputNumber = int(input("please try again"))

Or something to the effect that will allow the user to update their guess before the following iteration.

1 Comment

I just made it work, please check the code I added to my question.
0

Try this

def guesst(secretNumber):

numberOfGuesses = 0

while True:
    inputNumber = int(input('I\'m thinking of a number between 1 and 20, try to guess which one: '))
    if inputNumber > secretNumber:
        numberOfGuesses += 1
        print('Your guess is too high.')
    elif inputNumber < secretNumber:
        numberOfGuesses += 1
        print('Your guess is too low.')
    elif inputNumber == secretNumber:
        print('Your guess is correct, congratulations! You\'ve my number in ', numberOfGuesses, 'guesses.')
        break
else:
    print('Please enter a number between 1 and 20')


# Defining the variables used by the function
snum = 11
guesst(snum)

When the while loop repeats, you'll need to collect input from the user again, so your input statement should be in the while loop, not outside the function.

Your if statement should also be within the while loop.

1 Comment

Thank you for the corrections and insight, I appreciate it. I corrected my code before I saw your answer but I technically did the same thing as you suggested. You can check the code in my question above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.