Joshua Hook
Joshua Hook

Reputation: 9

Not understanding my python dilemma

I am trying to finish up an assignment for a class "grade determination"

Write a program that will input 3 test scores.The program should determine and display their average.The program should then display the appropriate letter grade based on the average.The letter grade should be determined using a standard 10-point scale: (A = 90-100; B = 80-89.999; C = 70-79.999, etc.)

So far what I've been able to put together, (and it works averaging)

def main():  
    score1 = input("What is the first test score? ")
    score2 = input("What is the second test score? ")
    score3 = input("What is the third test score? ")
    scoreaverage = score1 + score2 + score3
score = (scoreaverage / 3)
    if score < 60:
        grade="F"
elif score < 70:
        grade="C"
elif score < 80:
        grade="B"
elif score < 90:
        grade="A"
else:
        print score, " is the student's grade average!"


main()

If I replace score with grade I get an error.

raceback (most recent call last):
File "<stdin>", line 1, in <module>
  File "<stdin>", line 16, in main
UnboundLocalError: local variable 'grade' referenced before assignment

So my question is how do I get the letter grade to print correctly?

Upvotes: 0

Views: 814

Answers (3)

jurgenreza
jurgenreza

Reputation: 6086

Welcome to SO. As explained by others, your problem was accessing grade before assigning a value to it. Here is another suggestion for doing what you want to do: (see the comments)

#disregard next line if you are using Python 3.3
from __future__ import division

#use a list to store the scores
scores = []

#if you are using 2.7 replace input with raw_input
#use float() to convert the input to float
scores.append(float(input("First score? ")))
scores.append(float(input("Second score? ")))
scores.append(float(input("Third score? ")))

#More general way of calculating the average
score = sum(scores) / len(scores)

#You could use a dictionary for score-grade mapping
grades = {100: 'A', 90: 'A', 80: 'B',
          70: 'C', 60: 'D', 50: 'E',
          40: 'F', 30: 'F', 20: 'F',
          10: 'F', 0: 'F'}

#removes the remainder from the score
#to make use of the above dictionary
grade = grades[score - (score % 10)]

#Use string formatting when printing
print('grade: %s, score: %.2f' % (grade, score))

Upvotes: 3

tdelaney
tdelaney

Reputation: 77337

Your grade comparisons are off by 10 points. You won't assign anything to grade if score is between 90 and 100. And a piece of advice for a newbie programmer: Use a debugger. You'd have discovered the problem in just a few minutes had you stepped through it.

Try:

if score < 60:
    grade="F"
elif score < 70:
    grade="D"
elif score < 80:
    grade="C"
elif score < 90:
    grade="B"
else:
    grade="A"

Upvotes: 0

karthikr
karthikr

Reputation: 99620

def main():  
    score1 = input("What is the first test score? ")
    score2 = input("What is the second test score? ")
    score3 = input("What is the third test score? ")
    scoreaverage = score1 + score2 + score3
    score = (scoreaverage / 3)
    if score < 60:
        grade="F"
    elif score < 70:
        grade="C"
    elif score < 80:
        grade="B"
    elif score < 90:
        grade="A"
    else:
        print score, " is the student's grade average!"

main()

This would be your indented code. Are you sure you get the error you are mentioning? The following code would generate the error you are mentioning.

    print grade, " is the student's grade average!"

You can fix it this way:

def main():  
    score1 = input("What is the first test score? ")
    score2 = input("What is the second test score? ")
    score3 = input("What is the third test score? ")
    scoreaverage = score1 + score2 + score3
    score = (scoreaverage / 3)
    grade = 'Unknown'
    if score < 60:
        grade="F"
    elif score < 70:
        grade="C"
    elif score < 80:
        grade="B"
    elif score < 90:
        grade="A"

    print "%s is the student's grade average!" % grade

main()

Upvotes: 3

Related Questions