首页 > 解决方案 > 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")

预期是输入数字的打印时间表

标签: python

解决方案


这是错误的方式,永远不可能是真的,因为您要求tt同时小于 2 和至少 12:

if 2 >= tt >= 12:

你可能的意思是:

if 2 <= tt <= 12:

而不是elif使用条件,您可以使用else.


推荐阅读