简体   繁体   English

Python计算字母等级

[英]Python calculating letter grades

I'm having trouble getting the letter grade to output anything but F for any value of input that is put into the scores.对于放入分数的任何输入值,我无法获得字母等级以输出除 F 之外的任何内容。 Can anyone figure out what the problem is.任何人都可以弄清楚问题是什么。 It could be something in my if statement isn't correct but I am not sure if that is true.这可能是我的 if 语句不正确,但我不确定这是否正确。

score1=float(input("Enter score 1: "))
score2=float(input("Enter score 2: "))
score3=float(input("Enter score 3: "))
score4=float(input("Enter score 4: "))
score5=float(input("Enter score 5: "))
scores=[score1, score2, score3, score4, score5]

def determine_grade(scores):

    if score >=90:
        return 'A'
    elif score >=80:
        return 'B'
    elif score >=70:
        return "C"
    elif score >=60:
        return "D"
    elif score <60:
        return "F"

determine_grade(scores)

print("scores \t numeric grade \t letter grade") 
print("-------------------------------------------------------------") 
print("Score 1: ","\t", score1,"\t", determine_grade(score1))
print("Score 2: ","\t", score2,"\t", determine_grade(score2))
print("Score 3: ","\t", score3,"\t", determine_grade(score3))
print("Score 4: ","\t", score4,"\t", determine_grade(score4))
print("Score 5: ","\t", score5,"\t", determine_grade(score5))
print("-------------------------------------------------------------")
calc_average= sum(scores)/len(scores)
print("Average Score: {0:.2f}".format(calc_average))

First, function determine_grade() is expecting a float parameter;首先,函数determine_grade()需要一个float参数; but you passed a list of float scores .但是你通过了一个浮点scores列表。

Second, within function determine_grade() , there is a mismatch between parameter scores and score .其次,在函数determine_grade() ,参数scoresscore之间存在不匹配。

Moreover, the code can be written in a cleaner way:此外,代码可以用更简洁的方式编写:

num_scores = 5
scores = []
for i in range(num_scores):
    score = float(input("Enter score {}: ".format(i)))
    scores.append(score)


def determine_grade(scores):
    if score >= 90:
        return 'A'
    elif score >= 80:
        return 'B'
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    elif score < 60:
        return "F"


print("scores\tnumeric grade\tletter grade")
print('-' * 40)
for i in range(num_scores):
    print("Score {}:\t{}\t{}".format(i, scores[i], determine_grade(scores[i])))
print('-' * 40)
calc_average = sum(scores) / len(scores)
print("Average Score: {0:.2f}".format(calc_average))

You should say what the problem is and what the output looks like.您应该说明问题是什么以及输出是什么样的。 Having said that, from a quick glance, I think this is your problem.话虽如此,乍一看,我认为这是您的问题。

def determine_grade(scores):

    if score >=90:
        return 'A'
    elif score >=80:

In your determine_grade function you are passing 'scores' but inside it you are checking the variable 'score' which will throw up an "undefined" error.在您的determine_grade 函数中,您正在传递“分数”,但在其中您正在检查变量“分数”,这将引发“未定义”错误。

Also you don't need this line.你也不需要这条线。 You're not really using the return value.您并没有真正使用返回值。

determine_grade(scores)

Other than the issue pointed out by hikerjobs, the line除了hikerjobs指出的问题外,这条线

determine_grade(scores)

will not do what you want it to do, because in that scope, scores is a list, becaues of the line:不会做你想让它做的事情,因为在那个范围内, scores是一个列表,因为该行:

scores=[score1,score2,score3,score4,score5]

But your determine_grade function expects a float.但是您的determine_grade函数需要一个浮点数。

(On that note, I recommend you get into the habit of writing docstrings for your code. It will help catch a lot of simple errors.) (关于这一点,我建议您养成为代码编写文档字符串的习惯。这将有助于捕获许多简单的错误。)

How about this?这个怎么样?

grades = {60:'D',70:'C',80:'B',90:'A',100:'A'}成绩 = {60:'D',70:'C',80:'B',90:'A',100:'A'}

myscore = 72我的分数 = 72

print(grades.get(int(myscore/10)*10,'F'))打印(成绩.get(int(myscore/10)*10,'F'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM