使用我的代码

时间:2016-11-16 20:30:21

标签: python

我遇到了以下问题并要求使用python为它编写解决方案算法。

问题: 编写一个Python程序来确定平均成绩最高的学生。每个学生都参加期中考试和决赛。等级应验证在0到100之间(包括0和100)。输入每个学生的姓名和成绩,并计算学生的平均成绩。输出具有最佳平均值和平均值的学生姓名。

这是我的代码:

def midTerm():
    midtermScore = int(input("What is the midterm Score: "))
    while (midtermScore <= 0 or midtermScore >= 100):
        midtermScore = int(input("Please enter a number between 0 and 100: "))
    return midtermScore
def final():
    finalScore = int(input("What is the final Score: "))
    while (finalScore < 0 or finalScore > 100):
        finalScore = int(input("Please enter a number between 0 and 100: "))
    return finalScore

total = 0
highest = 0
numStudents = int (input("How Many Students are there? "))
while numStudents < 0 or numStudents > 100:
    numStudents = int (input("Please enter a number between 0 and 100? "))
for i in range (1, numStudents+1):
    students = (input("Enter Student's Name Please: "))
    score = (midTerm()+ final())
    total += score
avg = total/numStudents
if (highest < avg):
    highest = avg
    winner = students
print ("The Student with the higgest average is: ", winner, "With the highest average of: ", avg)

我遇到的问题是最后一部分。该程序不会打印平均值最高的人的姓名,而是打印最后输入的人的姓名。关于如何从这里前进,我感到非常困惑。你能帮忙吗?提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

你离我不远。看看这里:

for i in range (1, numStudents+1):
    students = (input("Enter Student's Name Please: "))
    score = (midTerm()+ final())
    total += score
avg = total/numStudents
if (highest < avg):
    highest = avg
    winner = students

除了缩进错误(希望只是笨拙的复制粘贴)你实际上并没有在任何地方计算每个学生的平均分数。尝试这样的事情:

for i in range (numStudents):
    student_name = (input("Enter Student's Name Please: ")) 
    student_avg = (midTerm() + final()) / 2 # 2 scores, summed and divided by 2 is their average score
    if (highest < student_avg):
        highest = student_avg
        winner = student_name  # save student name for later

print ("The Student with the higgest average is: ", winner, "With the highest average of: ", highest)

看起来你原本试图计算总类平均值,这不是问题陈述所描述的。希望这有帮助!