Python. while循环中的变量未更新。

6
我是一个新手程序员,我在编写一个基本的猜数字游戏时遇到了一个问题。 x是计算机生成的随机数。程序应该比较(previous_guess - x)的绝对值和新的猜测减去x的绝对值,并告诉用户他们的新猜测是否更接近或更远。 但是变量previous_guess并没有随着新值更新。 任何帮助都将不胜感激。
以下是目前的代码:
    ###Guessing Game
import random

n = 100
x = random.randint(1,n)
print("I'm thinking of a number between 1 and ", n)

##print(x) ## testing/cheating.
count = 0

while True:
    previous_guess = 0 # Should update with old guess to be compared with new guess
    guess = int(input("Guess the number, or enter number greater that %d to quit." % n))
    count += 1

    print(previous_guess)
    print("Guesses: ", count)

    if guess > n:
        print("Goodbye.")
        break

    elif count < 2 and guess != x: 
        print("Unlucky.")
        previous_guess = guess #####

    elif count >= 2 and guess != x:
        if abs(guess - x) < abs(previous_guess - x):
            previous_guess = guess #####

            print("Getting warmer...")
        else:
            previous_guess = guess #####
            print("Getting colder...")

    elif guess == x:
        print("You win! %d is correct! Guessed in %d attempt(s)." % (x,count))
        break 

阅读https://ericlippert.com/2014/03/05/how-to-debug-small-programs/,了解有关学习如何调试程序的技巧。 - Code-Apprentice
1
看一下Python Tutor可视化工具。对于需要数千步或有数百个变量的程序,你需要传统的调试器,但对于像这样的小程序,交互式可视化工具是一个惊人的方式来查看代码如何工作的。 - abarnert
我只是为了参考而留下这里。当然,正确的答案已经在这里了,但在我的情况下,是因为我输入了 x =+ 1 而不是 x += 1。 - silvio
4个回答

11

每次循环时,您之前的猜测都会被重新初始化。这是编程中非常常见的错误,所以您并不孤单!

将其更改为:

previous_guess = 0
while True:
   #Rest of code follows

当出现这种情况时,你应该考虑以下几点:

  1. 变量声明在哪里?
  2. 变量初始化在哪里?
  3. 变量使用在哪里?

如果你对这些术语不熟悉,没关系!查一下吧!作为程序员,你必须善于使用 Google 或搜索文档(或者向 Stack Overflow 上的人提问,看来你已经掌握了这个技巧)。

另外,在编写可用的代码方面,学习如何进行调试也非常重要。

搜索“Python 调试教程”,找到一篇容易理解的(确保你能够跟上教程),然后就可以开始调试了。


8

每次循环重新开始时,您将 previous_guess 重置为0,因此丢弃了实际的上一个猜测。相反,您需要:

previous_guess = 0
while True:
    guess = ....

2

while 循环之前,你需要初始化 previous guess。否则,它会被一遍又一遍地初始化。

你已经在多个地方更新了 previous guess。你可以让它更简单:

import random

n = 100
x = random.randint(1,n)
print("I'm thinking of a number between 1 and ", n)

##print(x) ## testing/cheating.
count = 0
previous_guess = 0  # Should update with old guess to be compared with new guess
while True:
    guess = int(input("Guess the number, or enter number greater that %d to quit." % n))
    count += 1

    print(previous_guess)
    print("Guesses: ", count)

    if guess > n:
        print("Goodbye.")
        break
    elif count < 2 and guess != x:
        print("Unlucky.")
    elif count >= 2 and guess != x:
        if abs(guess - x) < abs(previous_guess - x):
            print("Getting warmer...")
        else:
            print("Getting colder...")
    elif guess == x:
        print("You win! %d is correct! Guessed in %d attempt(s)." % (x,count))
        break
    previous_guess = guess  #####

0
在 while 循环之前,您需要初始化上一个猜测值,否则它会一遍又一遍地被初始化。您必须将上一个猜测值的值设置为计算机生成的 x 值,并且当您在循环后继续时,您必须简单地更新上一个猜测值到下一个值,就像这样:
  1. 在 while 之前添加 { previous_guess = x }
  2. 在 while 之后添加 { previous_guess += x }
###Guessing Game
import random

n = 100
x = random.randint(1,n)
print("I'm thinking of a number between 1 and ", n)

##print(x) ## testing/cheating.
count = 0
previous_guess = x
while True:
     # Should update with old guess to be compared with new guess
    previous_guess += x
    guess = int(input("Guess the number, or enter number greater that %d to quit." % n))
    count += 1

    print(previous_guess)
    print("Guesses: ", count)

    if guess > n:
        print("Goodbye.")
        break

    elif count < 2 and guess != x: 
        print("Unlucky.")
        previous_guess = guess #####

    elif count >= 2 and guess != x:
        if abs(guess - x) < abs(previous_guess - x):
            previous_guess = guess #####

            print("Getting warmer...")
        else:
            previous_guess = guess #####
            print("Getting colder...")

    elif guess == x:
        print("You win! %d is correct! Guessed in %d attempt(s)." % (x,count))
        break 

当你赢了的照片 当你输了的照片


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接