# Simple integer comparison problem

Discussion in 'Python' started by tom@finland.com, Apr 14, 2007.

1. ### Guest

Hi!
I ran in problem with simple exercise. I'm trying to get program to
return grade when given points but no matter what, I always get F.

def grader():
print "Insert points: "
points = raw_input('> ')
int(points)

if points > 89 and points <= 100:
return "A"
elif points > 89 and points <= 89:
return "B"
elif points > 69 and points <= 79:
return "C"
elif points > 59 and points <= 69:
return "D"
else:
return "F"

grade = grader()
print grade

, Apr 14, 2007

2. ### Jakub StolarskiGuest

On Apr 14, 5:19 pm, wrote:
> Hi!
> I ran in problem with simple exercise. I'm trying to get program to
> return grade when given points but no matter what, I always get F.
>
> def grader():
> print "Insert points: "
> points = raw_input('> ')
> int(points)
>
> if points > 89 and points <= 100:
> return "A"
> elif points > 89 and points <= 89:
> return "B"
> elif points > 69 and points <= 79:
> return "C"
> elif points > 59 and points <= 69:
> return "D"
> else:
> return "F"
>
> grade = grader()
> print grade

You should write:
points = int(points)

int returns value, not change in place.

If I can suggest you can use simpler if-statement:
if 89 < points <= 100:

Jakub Stolarski, Apr 14, 2007

3. ### Dan BishopGuest

On Apr 14, 10:19 am, wrote:
> Hi!
> I ran in problem with simple exercise. I'm trying to get program to
> return grade when given points but no matter what, I always get F.
>
> def grader():
> print "Insert points: "
> points = raw_input('> ')
> int(points)
>
> if points > 89 and points <= 100:
> return "A"
> elif points > 89 and points <= 89:
> return "B"
> elif points > 69 and points <= 79:
> return "C"
> elif points > 59 and points <= 69:
> return "D"
> else:
> return "F"
>
> grade = grader()
> print grade

You have a typo in the first "elif": "points > 89 and points <= 89" is
never true, so you'll get an "F" instead of a "B".

BTW, Python lets you write things like "80 <= points < 90".

Dan Bishop, Apr 14, 2007
4. ### Guest

Thanks for help!

, Apr 14, 2007
5. ### Bart WillemsGuest

> if points > 89 and points <= 100:
> return "A"
> elif points > 89 and points <= 89:
> return "B"
> elif points > 69 and points <= 79:
> return "C"
> elif points > 59 and points <= 69:
> return "D"
> else:
> return "F"

The previous posters already pointed out your int problem. However, the
if-statement can be written with a lot less clutter:

if points > 100:
return "Illegal score"
elif points > 89:
return "A"
elif points > 79:
return "B"
elif points > 69:
return "C"
elif points > 59:
return "D"
else:
return "F"

I have a feeling that there's a Python-solution that is shorter yet
better readable, I just can't figure it out yet...

Bart Willems, Apr 14, 2007
6. ### Anton VredegoorGuest

Bart Willems wrote:

> I have a feeling that there's a Python-solution that is shorter yet
> better readable, I just can't figure it out yet...

Shorter (and faster for big lists): Yes. More readable: I don't know, I
guess that depends on ones familiarity with the procedure.

import bisect

def grader(score):
c = bisect.bisect([60,70,80,90],score)
return 'FDCBA'[c]

A.

Anton Vredegoor, Apr 14, 2007

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.