0

我正在尝试完成班级“成绩确定”的作业

编写一个程序,输入 3 个测试分数。程序应确定并显示它们的平均值。然后程序应根据平均值显示适当的字母等级。字母等级应使用标准 10 分制确定:(A = 90-100;B = 80-89.999;C = 70-79.999 等)

到目前为止,我已经能够放在一起,(它的工作平均)

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()

如果我用成绩替换分数,我会得到一个错误。

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

所以我的问题是如何让字母等级正确打印?

4

3 回答 3

3
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()

这将是您的缩进代码。你确定你得到你提到的错误吗?以下代码将生成您提到的错误。

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

您可以通过以下方式修复它:

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()
于 2013-04-02T17:12:05.663 回答
3

欢迎来到 SO。正如其他人所解释的那样,您的问题是在为其分配值之前访问成绩。这是做你想做的事情的另一个建议:(见评论)

#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))
于 2013-04-02T18:21:34.223 回答
0

您的成绩比较差了 10 分。如果分数在 90 到 100 之间,您将不会为评分分配任何内容。对于新手程序员的一条建议:使用调试器。如果您逐步解决问题,您将在几分钟内发现问题。

尝试:

if score < 60:
    grade="F"
elif score < 70:
    grade="D"
elif score < 80:
    grade="C"
elif score < 90:
    grade="B"
else:
    grade="A"
于 2013-04-02T17:23:41.743 回答