def highlowR():
play_again = 1
while play_again==1:
ct = 0
guess = 50
x = 0
ans = ""
print "Lets play a game. \nThink of a number between 1 and 100.\n"
while ans!="c":
temp0 = 0
temp1 = 0
print "I guess %d" %guess
ans = raw_input("Am I too (h)igh, too (l)ow, or (c)orrect? \n")
if ans=="h":
temp0 = guess/2
temp1 = guess%2
guess = temp0 + temp1
elif ans=="l":
temp0 = guess/2
temp1 = guess%2
guess = guess + temp0 + temp1
elif ans=="c":
print "I got it! It only took me %d guesses." %ct
else:
print "I didn't quite understand what you meant there."
ct = ct+1
play_again = input("Would you like to play again? Yes = 1, No = 0: ")
print""
print "Thanks for playing!"
highlowR()
我几乎有一个正在运行的反向高低游戏,但我不知道如何更改我的 if 语句中的数学以优化我的结果。如果要猜测的数字是 1,它会起作用......但我不知道该怎么做才能优化我的结果。有什么帮助吗?