Python - 如何在 x 和 y 之间编写 if 语句?

8

我最近开始学习Python,虽然C ++很有趣,但Python看起来也很酷。我想让Python做一些事情,只要输入在特定数字范围内。

def main():
    grade = float(input("“What’s your grade?”\n:"))
    if grade >= 90:
        print("“You’re doing great!”")
    elif(78 >= grade <= 89):
        print("“You’re doing good!”")
    elif(77 >= grade > 65):
        print("You need some work")
    else:
        print("Contact your teacher")

main()

当我编写elif语句时,问题出现了,我无法使Python仅在成绩介于65和89之间时打印“doing great”语句。您如何处理数字范围?


@Frito 它在那里... - litelite
嘿,抱歉。我在您更新之前就看到了 :-) - Frito
1个回答

21

在Python中,您可以这样做来检查变量是否在特定范围内:

if 78 <= grade <= 89:
    pass

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接