私たちの課題は、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