gpt4 book ai didi

python - 高低: Computer guesses your number

转载 作者:行者123 更新时间:2023-12-01 04:41:07 26 4
gpt4 key购买 nike

全部。我对编程很陌生,我试图找出为什么我的代码不能正常工作。它会一直运行良好,直到您告诉计算机它的第一个猜测是否太高 (h) 或太低 (l)。比如说,如果猜测太高,并告诉计算机,之后的每次猜测都会继续猜测更低的值,无论您输入的值是否太低 (l)。反之亦然。希望有人可以提供帮助。这是代码!

import random
import time

print "Think of a number between 1 and 100 and I'll try and guess it."
time.sleep(2)
print "Let me know if my guess is too (h)igh, too (l)ow, or (c)orrect."
time.sleep(2)

guess = int(raw_input("Pick your number between 1-100: "))

low = 1
high = 100
tries = 0
compguess = random.randrange(low, high)
h = compguess > guess
l = compguess < guess
c = compguess == guess

while compguess != guess:
tries += 1
print "Is it", compguess

if h:
raw_input ()
new_high = (compguess - 1)
compguess = random.randint(low, new_high)

elif l:
raw_input ()
new_low = (compguess + 1)
compguess = random.randint(new_low, high)

elif c:
raw_input ()
print("The computer guessed your number of: ", guess)

请原谅间距。我不太确定如何正确复制它。

最佳答案

raw_input() 函数返回一个值。如果您没有将返回值分配给变量或以其他方式对其进行处理,则返回的值将被 Python 丢弃。

你可能想要这样的东西:

print "Is it", compguess
answer = raw_input()
if answer == "h":
...
elif answer == "l":
...

这样,您就可以提示用户输入,等待用户输入内容,然后根据用户输入的内容进行操作。

您也不需要 hlc 局部变量。它们实际上在您的代码中没有任何作用。

最后,为什么要让用户告诉计算机用户在想什么数字呢?这个练习的目的不就是让计算机在不知道结果是什么的情况下猜测用户的号码吗?

关于python - 高低: Computer guesses your number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30690149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com