hint = str
low = 0
high = 100
guess = (high + low)/2
answer = int(raw_input("Please think of a number between 0 and 100: "))
while (True):
print "Is your secret number " + str(guess) + "?"
hint = raw_input("H, L, or C: ")
hint = hint.lower()
while (hint != "h" and hint != "l" and hint != "c"):
print "invalid option"
hint = raw_input("H, L, or C: ")
hint = hint.lower()
if (hint == "h"):
low = guess
print "newlow: " + str(low)
print "newGuess: " + str(guess)
elif (hint == "l"):
high = guess
elif (hint == "c"):
print "Correct, the answer was " + str(answer)
break
为什么变量猜测值未更改,我希望将低值更改为50,因此newGuess会变为75,正确吗?
从程序进入while
循环的那一刻起,所有变量都将被设置,除非另行分配。
您已完成的工作将重新分配给low
变量。但是,由于循环已经具有的值(guess
使用的旧值)low
,因此您需要guess
使用新值重新分配。尝试将定义guess
放入第一个while
循环中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句