python - 为什么只是 python 代码中的增强赋值语句会破坏它?
问题描述
为什么这个python代码运行正常,虽然输出不正确
low = 1
high = 1000
print(f"I will guess a value from {low} and {high}"
"press 'h' or 'l' or 'c'for complete anything else to exit")
guesses = 1
while True:
mid = low + (high - low) // 2
print("is this is your guess {}".format(mid))
inp = input()
if inp == 'h':
low += 1
elif inp =='l':
high -= 1
elif inp == 'c':
print('got it')
else:
break
但是当我只添加这行代码时,它说语法无效并中断
low = 1
high = 1000
print(f"I will guess a value from {low} and {high}"
"press 'h' or 'l' or 'c'for complete anything else to exit")
guesses = 1
while True:
mid = low + (high - low) // 2
print("is this is your guess {}".format(mid))
inp = input()
if inp == 'h':
low += 1
elif inp =='l':
high -= 1
elif inp == 'c':
print('got it')
guesses += 1
else:
break
那么为什么guesses += 1
只是破坏一切它只是一个计数器它在代码中没有任何作用除了计算我循环了多少次
- 如果您还可以告诉我为什么此代码不打印打印预测值,将不胜感激该程序应该是二进制搜索程序。
解决方案
推荐阅读
- python - python列表具有字符和数字
- angular - Angular 5:材料表数据不会在从不同组件触发时更新
- windows - 函数调用后 ESI 和 EDI 改变值
- r - 在推荐实验室中使用用户 ID 进行预测
- flutter-layout - 主页背景和导航栏抖动
- javascript - 从 figcaption 获取 Imagelightbox 中 Ligthbox 的标题
- proxy - Nuxt Axios 动态网址
- android - Intent.createChooser 的 startActivity 中的 Android(共享按钮)崩溃适用于除像素外的大多数手机
- appium - UIAutomatorViewer 显示相同的资源 id
- java - Spring boot RestTemplate 发布 400 错误