简体   繁体   中英

Python Why does my code result in an infinite loop?

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)

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

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 input ing a new value.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM