该程序的工作原理如下:您(用户)认为0(包括)和100(不包括)之间的整数。计算机猜测,你给它输入 - 它的猜测是太高还是太低?使用二分搜索,计算机将猜测用户的密码!
我的代码:
high = 100
low = 0
correct = False
response = ""
user_number = input("Please think of a number between 0 and 100!")
while (response != "c"):
guess = int((high + low)/2)
print("Is your secret number", guess, "?")
response = 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 not (response == "h" or response == "c" or response == "l"):
print("Sorry, I did not understand your input.")
elif (response is "h"):
high = guess
elif (response is "l"):
low = guess
print ("Game over. Your secret number was:", guess)
目前EdX网站将我的答案标记为不正确,我通过尝试输入数字(如83,8,42)检查了输出,它正确显示为edX网站代码显示。有人可以给我一些关于我的代码有缺陷的建议吗?谢谢。