gpt4 book ai didi

python - 学习 Python : If condition executing all the time

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:44 25 4
gpt4 key购买 nike

我正在学习 python,我不明白为什么下面的程序打印出 your number is greater than what i thought 即使猜测的数字更小。它是否像在 Python 中一样,它比较的是字符串而不是数字?

number = 32
tries = 1

print "I have chosen a number, you have to guess it."
guess = raw_input ("Guess:")

while guess != number:
tries = tries+1
if guess > number:
print("Your guess is greater than i thought")
guess = raw_input ("Another Guess:")
else:
print("Your guess is smaller than i thought")
guess = raw_input ("Another Guess:")

print "You guess it right in %d turns." %tries

P.S:我知道这是一个愚蠢的问题,但我是 python 的菜鸟!

最佳答案

raw_input 将返回一个字符串。您需要将其解析为数字才能使比较正常工作。我认为这只是 int(raw_input(...))

关于python - 学习 Python : If condition executing all the time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6739453/

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