Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 = 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()

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?

like image 256
Nate Dukes Avatar asked Nov 08 '17 19:11

Nate Dukes


People also ask

How do you write an IF statement between two numbers in Python?

Another way to check if a number is between two numbers in Python is to use the Python range() function and check if the number is included in a created range. To create a range, you can pass two numbers to range(). Then you can use the in logical operator to check if a number is in the created range.

Can you use if statement twice Python?

Answer 514a8bea4a9e0e2522000cf1 You can use multiple else if but each of them must have opening and closing curly braces {} . You can replace if with switch statement which is simpler but only for comparing same variable.

How do you check if a number falls within a range in Python?

You can check if a number is present or not present in a Python range() object. To check if given number is in a range, use Python if statement with in keyword as shown below. number in range() expression returns a boolean value: True if number is present in the range(), False if number is not present in the range.

Can you use strings in IF statements Python?

We can perform string comparisons using the if statement. We can use relational operators with the strings to perform basic comparisons. See the code below.


1 Answers

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

if 78 <= grade <= 89:
    pass
like image 131
Karol Babioch Avatar answered Sep 27 '22 21:09

Karol Babioch