我正在编写一个程序来计算一个班级的平均成绩和字母等级。每次我运行代码时都会打印出#34;你的等级是A"。我觉得我的def average()函数有问题,但我无法弄明白。
它也只打印字母等级,而不是分数
homework1 = int(raw_input('Enter homework1 score: '))
homework2 = int(raw_input('Enter homework2 score: '))
quiz = int(raw_input('Enter quiz score: '))
exam = int(raw_input('Enter Final Exam Score: '))
if homework1 > 100:
print 'Score cannot be greater than 100'
if homework1 < 0:
print 'Score cannot be less than 0'
if homework2 > 100:
print 'Score cannot be greater than 100'
if homework2 < 0:
print 'Score cannot be less than 0'
if quiz > 100:
print 'Score cannot be greater than 100'
if quiz < 0:
print 'Score cannot be less than 0'
if exam > 100:
print 'Score cannot be greater than 100'
if exam < 0:
print 'Score cannot be less than 0'
numbers = [homework1, homework2, quiz, exam]
def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)
def get_average(numbers):
homework = average("homework1, homework2")
quiz = average("quiz")
exam = average("exam")
return .2*quiz + .2*exam + .6*homework
def get_letter_grade(score):
if score >= 93.333:
return "Your grade is an A"
elif score >= 90:
return "Your grade is an A-"
elif score >= 86.667:
return "Your grade is a B+"
elif (avg >= 83.333):
return "Your grade is a B"
elif (avg >= 80):
return "Your grade is a B-"
elif (avg >= 76.667):
return "Your grade is a C+"
elif (avg >= 73.333):
return "Your grade is a C"
elif (avg >= 70):
return "Your grade is a C-"
elif (avg >= 66.667):
return "Your grade is an D"
elif (avg >= 65):
return "Your grade is an D-"
else:
return "Your grade is a F"
if (avg == 0):
return "There is an error"
print get_letter_grade(get_average)
答案 0 :(得分:0)
调试代码后,我可以看到您将 get_average()(函数)传递给 get_letter_grade()作为参数。
print get_letter_grade(get_average)
这是第一件事。另外,Tom Karzes已经说过将字符串解析为 average()时明确需要一个数字列表。
答案 1 :(得分:0)
您每次都会收到“您的成绩是A”,因为您使用函数get_letter_grade
作为输入调用get_average
函数。
由于评估get_average > 93,333
始终为True
,因此您不会得到任何其他答案。
再来一次。
答案 2 :(得分:0)
我建议您查看代码。但要解决代码中的问题,可以这样做:
homework1 = int(raw_input('Enter homework1 score: '))
homework2 = int(raw_input('Enter homework2 score: '))
quiz = int(raw_input('Enter quiz score: '))
exam = int(raw_input('Enter Final Exam Score: '))
if homework1 > 100:
print 'Score cannot be greater than 100'
if homework1 < 0:
print 'Score cannot be less than 0'
if homework2 > 100:
print 'Score cannot be greater than 100'
if homework2 < 0:
print 'Score cannot be less than 0'
if quiz > 100:
print 'Score cannot be greater than 100'
if quiz < 0:
print 'Score cannot be less than 0'
if exam > 100:
print 'Score cannot be greater than 100'
if exam < 0:
print 'Score cannot be less than 0'
numbers = [homework1, homework2, quiz, exam]
def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)
def get_average(numbers):
# you accessed your array in the wrong way
homework = average(numbers[0:1])
quiz = average([numbers[2]])
exam = average([numbers[3]])
return .2*quiz + .2*exam + .6*homework
def get_letter_grade(numbers):
# here, you mixed up 'score' and 'avg' which were both not defined
score = get_average(numbers)
if score >= 93.333:
return "Your grade is an A"
elif score >= 90:
return "Your grade is an A-"
elif score >= 86.667:
return "Your grade is a B+"
elif (score >= 83.333):
return "Your grade is a B"
elif (score >= 80):
return "Your grade is a B-"
elif (score >= 76.667):
return "Your grade is a C+"
elif (score >= 73.333):
return "Your grade is a C"
elif (score >= 70):
return "Your grade is a C-"
elif (score >= 66.667):
return "Your grade is an D"
elif (score >= 65):
return "Your grade is an D-"
else:
return "Your grade is a F"
if (score == 0):
return "There is an error"
# You used to call the get_letter_grade with a (not called) function as an argument
print get_letter_grade(numbers)