-3

I am trying to write a program that will convert a score between 0.0 and 1.0 into a letter grade. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a letter grade using the following:

Score   Grade
>= 0.9  A
>= 0.8  B
>= 0.7  C
>= 0.6  D
<0.6    F

Requirements:

  1. Use the “input” command to take in user input for score
  2. check input to ensure that the score is in the rage of (0.0 to 1.0), if outside the rage - should output "Bad score"
  3. The supplied input is of type string by default, so it must be converted to the float type
  4. Should also catch a non-numeric input and print a “Bad score” error message.

This is the code I have as of now:

import sys
scores = input ("Enter your score: ")

try:
    floatScores = float(scores)
except:
    print ("Bad Score")

if floatScores >= 0.0 and floatScores < 0.4:
    print ("You have a: F")
elif floatScores >= 0.6 and floatScores < 0.7:
    print ("You have a: D")
elif floatScores >= 0.7 and floatScores < 0.8:
    print ("You have a: C")
elif floatScores >= 0.8 and floatScores < 0.9:
    print ("You have a: B")
elif (floatScores >= 0.9 and floatScores <= 1.0:
    print ("You have an: A")

else:
     print ("Bad Score")

sys.exit()

Please advice. Thanks

IronBat
  • 107
  • 2
  • 10

1 Answers1

0

I'm not going to do your homework for you, however, I will give you a few hints...

  1. Your first requirement states that you need to use the input command to take user input for the score. You know how to set a variable to an input, so start with that.

  2. Next, you need to check if the score is within 0.0-1.0. You can use an if statement checking if your input is greater than or equal to 0, and less than or equal to 1.

  3. For your third requirement, I recommend you read this post.

  4. For your fourth requirement, I recommend you use a try-except or assert functionality of Python.


Edit

Now that you've posted some code I can help you.

In your elif (floatScores >= 0.9 and floatScores <= 1.0: you don't need the ( so get rid of that.

Then your code will work fine! :)


Note

Here is a slightly different way to do it if you don't want that long chain of if-elifs.

def range_score():
    score_range = {'A': 0.9, 'B': 0.8, 'C': 0.7, 'D': 0.6, 'F': 0.0}
    try:
        score = float(input('Score? '))
    except ValueError:
        return 'Bad Score'

    assert 0.0 <= score <= 1.0, 'Bad Score'

    for k, v in score_range.items():
        if score >= v:
            return k
Community
  • 1
  • 1
Jason
  • 2,278
  • 2
  • 17
  • 25
  • 1
    Thank you! That was simple.. I am not sure how i missed the "(" Thanks for the correction. – IronBat Mar 27 '16 at 21:01
  • @IronBat If I helped you please mark this as the "best answer" using the gray checkmark underneath the voting on the answer! :) – Jason Mar 27 '16 at 21:01
  • I am getting an error when I enter a non-numeric value.. Not sure what it means. Can you tell me what could be causing that? Enter your score: six Bad Score Traceback (most recent call last): File "C:/Users/score.py", line 22, in if floatScores >= 0.0 and floatScores < 0.6: NameError: name 'floatScores' is not defined – IronBat Mar 27 '16 at 22:27
  • It's because after you catch the error the program continues without `floatScores` being defined. I recommend you put a break at the end of your `except: print('Bad score')`. So `except: print('Bad Score') break` – Jason Mar 27 '16 at 22:30
  • This code is not robust. `dict` is not an ordered collection, so you have no guarantee that the (key, value) pairs will be returned by `score_range.items()` in the same order as they have in the `dict` literal. Try this: `print({'0':0, '1':1, '2':2, '3':3, '4':4})`; the results probably won't be in numerical order, and may vary from run to run depending on Python version, or if the `-R` option is given on the command line in older Python versions. – PM 2Ring Apr 19 '16 at 11:19