HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Grading scores from 0.0 to 1.0

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gradingfromscores

Problem

Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:

`Score | Grade
-------+------
>= 0.9 | A
>= 0.8 | B
>= 0.7 | C
>= 0.6 | D


If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.

try:
    inp = raw_input("Enter a number between 0.0 and 1.0: ")
    score = float(inp)
    if (score >= 1.0):
        print("You didn't follow instructions")
        exit()
    elif (score >= 0.9):
        print("A")
    elif (score >= 0.8):
        print("B")
    elif (score >= 0.7):
        print("C")
    elif (score >= 0.6):
        print("D")
    else:
        print("F") 
except:
    print("Please enter numerical numbers")
    exit()

Solution

Yes, your code is correct, although I would recommend adding comments, and following the style guidelines of PEP8. Here are a few tips I have.

  • You should specify what type of error you want to except. If you just type except:, other errors could go un-noticed.



  • Typing exit() really isn't needed here. The program will terminate either way.



  • You don't need to put parentheses around conditionals in if statements.



  • It seems also that you're using Python2, so no parentheses are needed for print.



  • You could make this into a function that returns the grade.



Here's my re-factored version of the code.

def return_grade(user_input):
    """
    Return the user's grade based on the following table.
    Score  | Grade
    -------+------
    >= 0.9 | A
    >= 0.8 | B
    >= 0.7 | C
    >= 0.6 | D
     1.0:
            return "Please enter a number between 0.0 and 1.0"

        elif user_score >= 0.9:
            return "Grade: A"

        elif user_score >= 0.8:
            return "Grade: B"

        elif user_score >= 0.7:
            return "Grade: C"

        elif user_score >= 0.6:
            return "Grade: D"

        elif user_score = 0.0:
            return "Grade F"
    except ValueError:
        print("Please enter a number.")

print return_grade(raw_input("Enter a number between 0.0 and 1.0"))

Code Snippets

def return_grade(user_input):
    """
    Return the user's grade based on the following table.
    Score  | Grade
    -------+------
    >= 0.9 | A
    >= 0.8 | B
    >= 0.7 | C
    >= 0.6 | D
    < 0.6 | F
    """
    try:
        user_score = float(user_input)
        if user_score > 1.0:
            return "Please enter a number between 0.0 and 1.0"

        elif user_score >= 0.9:
            return "Grade: A"

        elif user_score >= 0.8:
            return "Grade: B"

        elif user_score >= 0.7:
            return "Grade: C"

        elif user_score >= 0.6:
            return "Grade: D"

        elif user_score < 0.6 and user_score >= 0.0:
            return "Grade F"
    except ValueError:
        print("Please enter a number.")


print return_grade(raw_input("Enter a number between 0.0 and 1.0"))

Context

StackExchange Code Review Q#88067, answer score: 12

Revisions (0)

No revisions yet.