0

I wrote a grade calculator where you put a float in and get a grade based on what you scored. The problem I have is that I belive I need a float(input... But that becomes an error if you write letters in the box...

def scoreGrade():
"""
Determine the grade from a score
"""
gradeA = "A"
gradeB = "B"
gradeC = "C"
gradeD = "D"
gradeF = "F"

score = float(input("Please write the score you got on the test, 0-10: "))
if score >= 9:
    print("You did really good, your grade is:", gradeA, ". Congratulations")
elif score >= 7:
    print("Your results are good. They earn you a:", gradeB, ". Better luck next time")
elif score >= 5:
    print("Not too bad. You got a:", gradeC)
elif score >= 4:
    print("That was close...:", gradeD)
elif score < 4:
    print("You need to step up and take the test again:", gradeF)
else:
    print("Grow up and write your score between 0 and 10")

Is there a way to get rid of the float and print the last statement if you write something else that the score from 0-10?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Thomas Bengtsson
  • 399
  • 2
  • 10
  • 22
  • It's called "if statement", not "if-loop" :) – André Laszlo Mar 02 '15 at 18:45
  • 3
    http://stackoverflow.com/questions/5424716/python-how-to-check-if-input-is-a-number-given-that-input-always-returns-stri – marsh Mar 02 '15 at 18:45
  • @marsh that's a good start, but I think this question also includes how to keep from repeating that `print` if the value is truly a number but it's outside of the valid range. – Mark Ransom Mar 02 '15 at 18:47
  • 2
    I deleted the unnecessary gendered slur. There's no need for language like that around here. – Daniel Roseman Mar 02 '15 at 18:47
  • I am British. The term is still offensive – Daniel Roseman Mar 02 '15 at 19:06
  • Sorry if I used a profanity. That was not my meaning but as @msw says, I'm using it in a none-offensive manner. But I'll keep the tone down from now on. As I'm Swedish, raised on Black Adder, Monty Python and Top Gear I might slip from time to time... – Thomas Bengtsson Mar 02 '15 at 19:11

3 Answers3

2

Something like this:

score = None
while score is None:
    try:
        score = float(input("Please write the score you got on the test, 0-10: "))
    except ValueError:
        continue

Keep on asking until the float cast works without raising the ValueError exception.

xnx
  • 24,509
  • 11
  • 70
  • 109
0

You could do

try:
    score = float(input("Please write the score you got on the test, 0-10: "))
except ValueError:
    print("Grow up and write your score between 0 and 10")
    scoreGrade()
Ben Morris
  • 606
  • 5
  • 24
0

I would suggest to use EAFP approach and separate handling good and bad inputs.

score_as_string = input("Please write the score you got on the test, 0-10: ")
try:
    score_as_number = float(score_as_string)
except ValueError:
    # handle error
else:
    print_grade(score_as_number)

def print_grade(score):
"""
Determine the grade from a score
"""
gradeA = "A"
gradeB = "B"
gradeC = "C"
gradeD = "D"
gradeF = "F"

if score >= 9:
    print("You did really good, your grade is:", gradeA, ". Congratulations")
elif score >= 7:
    print("Your results are good. They earn you a:", gradeB, ". Better luck next time")
elif score >= 5:
    print("Not too bad. You got a:", gradeC)
elif score >= 4:
    print("That was close...:", gradeD)
elif score < 4:
    print("You need to step up and take the test again:", gradeF)
else:
    print("Grow up and write your score between 0 and 10")

Note that typically you want to return from functions, not print inside them. Using function output as part of print statement is detail, and function does not have to know that.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93