python - if语句中的for循环,被跳过
问题描述
当我用 2 到 12 之间的数字运行我的代码时,要求的结果没有出现,但 elif 确实有效。所以我很难过如何解决这个问题。我的老师希望我加入一个 if 语句。
我已经尝试寻找以不同方式使用此 if 语句的方法,但我最终感到困惑。
tt = int(input("What times tables would you like: "))
if 2 >= tt >= 12:
for x in range(1, 13):
aw = tt * x
print(tt, "x", x, " = ", aw)
elif tt < 2 or tt > 12:
print("Please enter number between 2 and 12")
预期是输入数字的打印时间表
解决方案
这是错误的方式,永远不可能是真的,因为您要求tt
同时小于 2 和至少 12:
if 2 >= tt >= 12:
你可能的意思是:
if 2 <= tt <= 12:
而不是elif
使用条件,您可以使用else
.
推荐阅读
- jupyter-lab - 安装扩展后构建 Jupyter-lab 后如何返回正常侧边栏?
- angular - 浏览到 Angular 站点时,IE 文档模式设置为 7
- postgresql - 使用 plpgsql 捕获警告
- python - 椭圆对象上的 Tkinter 画布绑定正在阻塞类析构函数
- java - 如何在通知点击时返回应用程序
- reactjs - 如何测试反应路由器路由?
- java - 有什么方法可以拆分通量流吗?
- python - 这个字典是如何构造的?
- amazon-web-services - AWS 中是否有办法接收应用程序单独执行的成本明细?
- c# - 计算数组中的每个元素并显示哪个数字出现了多少次?