Velocity Reviews > Simple integer comparison problem

# Simple integer comparison problem

tom@finland.com
Guest
Posts: n/a

 04-14-2007
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.

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"

Jakub Stolarski
Guest
Posts: n/a

 04-14-2007
On Apr 14, 5:19 pm, (E-Mail Removed) 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.
>
> 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"
>

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:

Dan Bishop
Guest
Posts: n/a

 04-14-2007
On Apr 14, 10:19 am, (E-Mail Removed) 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.
>
> 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"
>

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".

tom@finland.com
Guest
Posts: n/a

 04-14-2007
Thanks for help!

Bart Willems
Guest
Posts: n/a

 04-14-2007
> 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"

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...

Anton Vredegoor
Guest
Posts: n/a

 04-14-2007
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

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

A.