简体   繁体   English

Python 为什么我的代码会导致无限循环?

[英]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.因此,我正在学习 Python 3。我尝试编写一个脚本,要求您猜测 1 到 20 之间的数字。现在我知道这不是最好的方法,但是当我运行代码时,它会进入无限环形。 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? secretNumber 不变? Use numberOfGuesses in the if and elififelif中使用 numberOfGuesses

Read these two lines:阅读这两行:

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

The while loop body doesn't change inputNumber . while循环体不会改变inputNumber If it is in the range 1..10, or 12..20, your loop amounts to while True: and it will run forever.如果它在 1..10 或 12..20 范围内,则您的循环相当于while True:并且它将永远运行。 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.您可能希望在循环内部进行if测试,并且您肯定希望在您返回评估循环条件时更改该值,例如通过input一个新值。

The variable inputNumber isn't updated within the while loop.变量 inputNumber 不会在 while 循环中更新。

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.当 while 循环重复时,您需要再次收集用户的输入,因此您的输入语句应该在 while 循环中,而不是在 function 之外。

Your if statement should also be within the while loop.您的 if 语句也应该在 while 循环内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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