← Back to all posts
##### How do I do this

Im doing an if statement where if a users grade on a test is between two percentages it is a certain grade like if a user gets 91 percent it's an A minus, and so I was coding and I got a problem where I tried to do...

elif percentage >= 90 and <95:

I think it is because of the and statement but I don't know how to solve this I need some help

hotnewtop

# Solution

try this

``````letter_grade = None
percentage = 0

if percentage >= 90 and percentage < 95:
elif percentage >= 80 and <85:
else:
percentage = "C-"``````

and so on.
That should work

JavaTime (1)

There is a better way to do this. For the sake of example I am going to say the following:

A = 100 - 91
B = 90 - 81
C = 80 - 71 etc.

The way I would code this is like this:

or you could do it the other way around, starting from the bottom, such as:

Tell me if this helps.

Kavin1983 (1)

@JavaTime This is actually very helpful because I forgot that python will only go to the next elif statement if the statement is false. Thanks alot

Kavin1983 (1)

thanks I appreciate all the help

Coder100 (17102)

Yes, you must use two comparisons, like this:

``````if ...:
...
elif percentage >= 90 and percentage < 95:
...``````

just like that, very easy.

IntellectualGuy (714)

Here, the problem is that it's elif not if

``````percentage = 93
``````percentage = 91