Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I kind of have an idea what to do, but I can't figure it out. The program needs to be able to get the students name and three test scores, then get the average score (percentage) out of the three scores. After that, you need to convert the score (percentage) into a grade.


Grading Scale:

Grading Scale


input ('Please press "Enter" to begin')

while True:
    import math

    studentName = str(input('Enter your Name: '))
    firstScore = int(float(input('First test score: ').replace('%', '')))
    secondScore = int(float(input('Second test score: ').replace('%', '')))
    thirdScore = int(float(input('Third test score: ').replace('%', '')))
    scoreAvg = (firstScore + secondScore + thirdScore) / 3

    def grade():
        if scoreAvg >= 93 and <= 100:
            return 'A'
        if scoreAvg <= 92.9 and >= 89:
            return 'A-'
        if scoreAvg <= 88.9 and >= 87:
            return 'B+'
        if scoreAvg <= 86.9 and >= 83:
            return 'B'
        if scoreAvg <= 82.9 and >= 79:
            return 'B-'
        if scoreAvg <= 78.9 and >= 77:
            return 'C+'
        if scoreAvg <= 76.9 and >= 73:
            return 'C'
        if scoreAvg <= 72.9 and >= 69:
            return 'C-'
        if scoreAvg <= 68.9 and >= 67:
            return 'D+
        if scoreAvg <= 66.9 and >= 60:
            return 'D'
        return 'F'

    print(grade[scoreAvg])
    print(studentName, "test score is: ",scoreAvg,'%')


    endProgram = input ('Do you want to restart the program?')

    if endProgram in ('no', 'No', 'NO', 'false', 'False', 'FALSE'):
        break
share|improve this question
1  
"How do I add..." questions are off-topic here. –  Winston Ewert Feb 4 '13 at 18:42
add comment

closed as off topic by Corbin, Yuushi, palacsint, Glenn Rogers, svick Feb 4 '13 at 16:16

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

It might be worth having an other look at the answers of your previous question.

As for the new comments :

Errors :

  • You forgot a ' after D+

  • I didn't run your code but the way you use grade() doesn't look right.

Style and performance :

  • You should import at the very beginning.

  • You can define the grade function before the loop and pass it the score as an argument.

  • If your process the different grades in order, you have only one limit to check.

Input :

  • You could check the values from the user (are they actually between 0 and 100?). Also, you could decide to make your code work for n scores (for any n) instead of 3.

Output :

  • The output seems weird to be but this is up to you.

Here's what the code looks like for me at the moment :

import math

input ('Please press "Enter" to begin')

def grade(scoreAvg):
    if scoreAvg >= 93:
        return 'A'
    if scoreAvg >= 89:
        return 'A-'
    if scoreAvg >= 87:
        return 'B+'
    if scoreAvg >= 83:
        return 'B'
    if scoreAvg >= 79:
        return 'B-'
    if scoreAvg >= 77:
        return 'C+'
    if scoreAvg >= 73:
        return 'C'
    if scoreAvg >= 69:
        return 'C-'
    if scoreAvg >= 67:
        return 'D+'
    if scoreAvg >= 60:
        return 'D'
    return 'F'

while True:
    studentName = str(input('Enter your Name: '))
    firstScore = int(float(input('First test score: ').replace('%', '')))
    secondScore = int(float(input('Second test score: ').replace('%', '')))
    thirdScore = int(float(input('Third test score: ').replace('%', '')))
    scoreAvg = (firstScore + secondScore + thirdScore) / 3
    print(grade(scoreAvg))
    print(studentName, "test score is: ",scoreAvg,'%')

    endProgram = input ('Do you want to restart the program?')

    if endProgram in ('no', 'No', 'NO', 'false', 'False', 'FALSE'):
        break

(Other things could be improved but that's for a beginning)

share|improve this answer
add comment

To avoid this rather long if/then/else handling, try with:

import bisect

thresholds = [       60,  67,  69,   73,   77,   79,   83,  87,   89,   93]
grades     = [ 'E', 'D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A']

def get_grade_for_score(score):
    if not 0 <= score <= 100:
        return 0 // do something, raise exception or whatever
    return grades[bisect.bisect(thresholds, score)]

See http://docs.python.org/2/library/bisect.html (there is even a grade example at the bottom of the page)

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.