Я запустил ваш код с кучей отпечатков в нем, чтобы проверить переменные во время выполнения.Это поведение
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:
.