我正在学习 python,我不明白为什么下面的程序会打印你的数字比我想象的要多即使猜测的数字较小。就像在 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(...))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)