Simple integer comparison problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tom@finland.com

    #1

    Simple integer comparison problem

    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
  • Jakub Stolarski

    #2
    Re: Simple integer comparison problem

    On Apr 14, 5:19 pm, t...@finland.co m 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:

    Comment

    • Dan Bishop

      #3
      Re: Simple integer comparison problem

      On Apr 14, 10:19 am, t...@finland.co m 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".

      Comment

      • tom@finland.com

        #4
        Re: Simple integer comparison problem

        Thanks for help!

        Comment

        • Bart Willems

          #5
          Re: Simple integer comparison problem

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

          Comment

          • Anton Vredegoor

            #6
            Re: Simple integer comparison problem

            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.

            Comment

            Working...