首页 > 解决方案 > 查找某个数字的平均值不起作用

问题描述

print("Please think of a secret number between 0 and 100")
low = 0
high = 100
average = low + high / 2

print("Is your number " + str(average) + "?")
guess = input ("If not enter 'h' if it is too high, 'l' if it is too low, 'c' if it was correct\n")
if guess == "l":
    low = average
    print("Is your number " + str(low + high/2) + "?")

我还没有完成程序,但是当打印程序并输入“l”时,它并没有像我想要的那样打印出 75。

标签: python

解决方案


这是数学:low+high/2是不同的(low+high)/2,只需average用好的公式再次计算

print("Please think of a secret number between 0 and 100")
low = 0
high = 100
average = (low + high) / 2

print("Is your number " + str(average) + "?")
guess = input("If not enter 'h' if it is too high, 'l' if it is too low, 'c' if it was correct\n")
if guess == "l":
    low = average
    average = (low + high) / 2
    print("Is your number " + str(average) + "?")

推荐阅读