在将<=与“ elif”和“ and”一起使用时遇到麻烦

时间:2019-01-15 17:35:16

标签: python python-3.x

使用教科书和代码课程学习Python。

此代码返回SyntaxError。我不能在同一elif语句中使用两个大于/小于符号吗?还是我的问题<< ='?

def movie_rating(rating):
    if rating <= 5:
        return "Avoid"
    elif rating > 5 and <= 9:
        return "I recommend!"
    else:
        return "Amazing"

1 个答案:

答案 0 :(得分:0)

The problem is because of your and in line 4, this error happening because when you use and you need to define a new condition in other word you should change your code to one of this codes:

def movie_rating(rating):
    if rating <= 5:
        return "Avoid"
    elif rating > 5 and rating <= 9:
        return "I recommend!"
    else:
        return "Amazing"

or change your code to this:

def movie_rating(rating):
    if rating <= 5:
        return "Avoid"
    elif 5> rating <= 9:
        return "I recommend!"
    else:
        return "Amazing"

if you want to know how you can use and in python, follow this link:

How to use boolean 'and' in Python and how use if and else elif or