在这个程序中,计算机会猜测数字。在游戏开始之前,计算机会询问获得了多少猜测。如果计算机丢失,它将询问正确的答案是什么。它还会检查以确保答案是合法的,并指出答案是否正确。如果用户给出的答案不一致,则计算机会指出并停止播放。
我的问题是,当我运行该程序时,如果计算机询问我的数字是高/低/还是50,那么我说“高”还是“低”,我以后可以说我的数字是50,它告诉我我赢了,而不是说“那不可能;您说的是高于/低于50!”我该如何解决这个问题?
print("Think of a number between 1 and 100 and I'll guess it.")
total = int(input("How many guesses do I get? "))
h = "higher"
l = "lower"
s = "same"
low = 0
high = 100
hls = ""
guess_count = 0
average = 0
while guess_count < total and hls != s:
average = (high + low) // 2
hls = input("Is the number higher, lower, or the same as " + str(average) + "? ")
if hls == h:
low = average
elif hls == l:
high = average
guess_count += 1
if high - low == 1:
break
if high - low == 1:
print("Wait; how can it be both higher than " + str(low) + " and lower than " + str(high) + "?")
elif hls == s:
print("I won!")
else:
answer = int(input("I lost; what was the answer? "))
if answer < low:
print("That can't be; you said it was higher than " + str(low) + "!")
elif answer > high:
print("That can't be; you said it was lower than " + str(high) + "!")
elif answer != average:
print("Well played!")
答案 0 :(得分:1)
这里的问题是,为了使计算机程序知道您对某个值给出了错误的响应(例如说“ 50太低”,而实际上答案是50),则需要记录其猜测以及您对这些猜测的回答。
因此,在进行猜测并获得“较低”或“较高”的响应后,可以将猜测放入low_guesses
或high_guesses
列表中,然后在末尾检查这些列表游戏的。您可能会遇到这样的事情:
low_guesses = []
high_guesses = []
while True: # breaks out when user types "same"
response = input("Is the number higher, lower, or the same as " + str(guess) + "? ")
if response == "lower":
# Add the guess to the low_guesses array:
low_guesses.append(guess)
elif response == "higher":
# Add the guess to the high_guesses array:
high_guesses.append(guess)
else:
# Secret number found, so break out of loop:
break
# Outside of the loop, examine your low_guesses and high_guesses lists.
您可以通过确保low_guesses
的所有元素都小于秘密数字,并且确保high_guesses
的所有元素都大于秘密数字来检查列表。如果那不是真的,那您就知道出了点问题。
(另外,一些建议:请不要命名变量l
或lst
。它们看起来很像1
或1st
,以致于很难阅读代码来读取,即使阅读您的代码的人已经知道它们代表变量名。)
答案 1 :(得分:1)
我在代码中运行了一堆打印程序,以检查执行时的变量。这种行为
Think of a number between 1 and 100 and I'll guess it.
How many guesses do I get? 5
Is the number higher, lower, or the same as 50? lower
High is: 50, low is: 0, average is: 50
Is the number higher, lower, or the same as 25? higher
High is: 50, low is: 25, average is: 25
Is the number higher, lower, or the same as 37? higher
High is: 50, low is: 37, average is: 37
Is the number higher, lower, or the same as 43? higher
High is: 50, low is: 43, average is: 43
Is the number higher, lower, or the same as 46? higher
High is: 50, low is: 46, average is: 46
I lost; what was the answer? 50
High is: 50, low is: 46, average is: 46
Well played!
因此,当计算机丢失且average
为46
时,它将通过以下条件运行:
if answer < low:
print("That can't be; you said it was higher than " + str(low) + "!")
elif answer > high:
print("That can't be; you said it was lower than " + str(high) + "!")
elif answer != average:
print("Well played!")
low
为46
,答案为50
,因此第一个条件为False
。 high
是50
,我的回答是50
,所以第二个条件是False
。但是,average
是46
,不等于我的answer
,即50
。 Well played!
是最终结果。
将elif answer > high:
更改为elif answer >= high:
,您将获得预期的结果。然后将elif answer < low:
更改为elif answer <= low:
。