Python- How to make an if statement between x and y? [duplicate]


April 2019


1.6k time


This question already has an answer here:

I've recently been breaching out to Python, as C++ is fun and all, but python seems kinda cool. I want to make Python do something as long as the input is between a certain number range.

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

The problem arises when I'm making the elif statement, I can't make it so Python only prints the "doing great" statement as long as the grade is between 65 and 89. How would you go about doing ranges of numbers?

1 answers


In Python you can do something like this to check whether an variable is within a specific range:

if 89 <= grade <= 78: