Simple integer comparison problem

T

tom

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
 
J

Jakub Stolarski

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:
 
D

Dan Bishop

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

Bart Willems

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

Anton Vredegoor

Bart said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top