简体   繁体   English

如何修复在While循环中不更新的变量(Python)

[英]How to fix variable not updating in while loop (python)

I am making a program that guesses a number that you are thinking of by telling the program if the number is higher or lower. 我正在编写一个程序,通过告诉程序该数字是高还是低来猜测您正在考虑的数字。 I'm not getting errors, but I can't seem to update the variable "guess" in the while loop, it remains equal to 50. The formula for higher-lower is given by the textbook, so I can't change that. 我没有收到错误,但是我似乎无法在while循环中更新变量“ guess”,它仍然等于50。教科书给出了高-低的公式,所以我无法更改。

I have tried moving the variable inside of the while loop, but still, it does not update. 我试过在while循环内移动变量,但是仍然不会更新。

print('Hello.')
print('Pick a secret number between 0 and 100.')
low = 0
high = 101
guess = 50
while True:
    print('Is your secret number',guess)
    use = input('Enter yes/higher/lower:\n').lower()
    if use == 'yes':
        print('Great!')
        break
    elif use == 'higher':
        low = guess
        guess = (guess-low)//2+low
    elif use == 'lower':
        high = guess
        guess = (high-guess)//2+guess
    else:
        print('I did not understand.')

IIUC, low , high , and guess needs to be updated for each loop. 需要为每个循环更新IIUC, lowhighguess Your new guess should be the mean of your new low and high . 您的新guess应该是你的新的平均值lowhigh

As is, your guess remains the same. 照原样,您的猜测保持不变。 For instance if the user responds with 'higher' , guess-low is 0 . 例如,如果用户以'higher'响应,则guess-low值为0 Divide by 2 is still 0 , then add by low , which is guess . 除以2仍为0 ,然后乘以low ,这是guess

You likely want this: 您可能想要这样:

low = guess
guess = (high + low) // 2

and

high =  guess
guess = (high + low) // 2

That's weird, because there's something wrong with your formula. 太奇怪了,因为您的公式有问题。

    elif use == 'higher':
        low = guess
        guess = (guess-low)//2+low
    elif use == 'lower':
        high = guess
        guess = (high-guess)//2+guess

In this part, since low == high == guess , the result of guess - low and high - guess will always be 0. Dividing that by 2 also gives 0. Therefore, both lines become equivalent to guess = guess . 在这一部分中,由于low == high == guessguess - lowhigh - guess low == high == guess的结果始终为0。除以2也会得到0。因此,两行都等同于guess = guess

This is because you are reassigning to low and high , which I believe you mean to hold the lower and upper limits of the guessing range. 这是因为您要重新分配到lowhigh ,我相信您的意思是保持猜测范围的下限和上限。

Perhaps you mean guess += (high - guess) // 2 and guess -= (guess - low) // 2 ? 也许您的意思是guess += (high - guess) // 2guess -= (guess - low) // 2

Your problem seems to be variable 'value reallocation' (I'm not sure for this word I'm French), with Equals : 您的问题似乎是带有“等于”的变量“值重新分配”(我不确定这个词我是法语):

= =

you must use 你必须使用

guess += value 猜+ =值

or 要么

guess = guess + guess 猜=猜+猜

or 要么

guess = guess - guess 猜=猜-猜

or 要么

guess -= guess 猜-=猜

That's because the logic you used is wrong. 那是因为您使用的逻辑是错误的。

Right now, what's happening is, 现在发生的是

low = guess
guess = (guess - low) // 2 + low

As low = guess the above statement is equivalent to, low = guess上面的陈述等同于,

guess = (guess - guess) // 2 + low
# guess = 0 + low
# guess = low

Similarly, for high, 同样,对于高

high = guess
guess = (high - guess) //2 + guess

As high = guess the above statement is equivalent to, high = guess上面的陈述等同于,

guess = (high - high) // 2 + guess
# guess = 0 + guess
# guess = guess

That's why it's always stuck on 50 . 这就是为什么它总是停留在50


The actual logic for it to work is as follows, 它起作用的实际逻辑如下,

elif use == 'higher':
    low = guess
    guess = (guess + high) // 2
elif use == 'lower':
    high = guess
    guess = (guess + low) // 2

Change the snippet to this. 将代码段更改为此。 It will work! 会工作的!


Hope this helps! 希望这可以帮助! :) :)

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

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