我们的任务是编写一个代码,它会从 0 到 100 猜测一个秘密数字。这是我的一段代码:
low = 0
mid = 50
high = 100
secretnum = "Is your secret number " + str(mid) + "?"
print"Please think of a number between 0 and 100!"
print secretnum
herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
while herp != 'c':
if herp == 'h':
high = mid
mid = int((mid + low)/2)
elif herp == 'l':
low = mid
mid = int((mid + high)/2)
else:
print"Sorry, I did not understand your input."
print secretnum
herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
if herp == 'c':
print "Game over. Your secret number was: " + str(mid)
这是输出:
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c
Game over. Your secret number was: 37