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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. Dan Bishop Guest

    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
    #3
  4. Guest

    Thanks for help!
    , Apr 14, 2007
    #4
  5. Bart Willems Guest

    > 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
    #5
  6. 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
    #6
    1. Advertising

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.
Similar Threads
  1. sieg1974
    Replies:
    8
    Views:
    786
    Joona I Palaste
    Sep 14, 2003
  2. Frederick Gotham
    Replies:
    2
    Views:
    398
    Frederick Gotham
    Jul 7, 2006
  3. Gary Wessle
    Replies:
    4
    Views:
    627
    Ian Collins
    Jul 29, 2006
  4. Deepu
    Replies:
    1
    Views:
    237
    ccc31807
    Feb 7, 2011
  5. db
    Replies:
    7
    Views:
    260
Loading...

Share This Page