python - 查找某个数字的平均值不起作用
问题描述
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。
解决方案
这是数学: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) + "?")
推荐阅读
- symfony - 语法错误 - 第 0 行,第 469 列:错误:预期的已知函数,得到 'IF'
- python - 使用 Python 识别 unicode 字符串中表情符号的最快方法
- angular6 - 如何在 Angular 6 指令中获取 img.naturalWidth?
- python - python 装饰器 TypeError 缺少 1 个必需的位置参数
- html - 从 html 文件调用 react js 函数
- sql - 逗号分隔的字符串匹配
- symfony - 如何在 Homestead 中运行旧版本的 Symfony(2/3)?
- php - preg_replace 超过 3 位的所有小数
- flutter - 如何在颤动中强制应用程序进入肖像模式
- javascript - 使用 select2.js 发送 ajax 请求