我给出了以下问题,并要求使用python为它编写解决方案算法。用我的代码遇到一些逻辑错误
问题: 编写一个Python程序来确定平均得分最高的学生。每个学生都需要一个中期和一个决赛。等级应该在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)
我遇到的问题是最后一部分。该程序不会打印出平均值最高的人的姓名,而是最后输入的人的姓名。我对如何从这里前进感到困惑。你能帮忙吗?预先感谢您的帮助。
看看下面这行:'students =(input(“Enter Student's Name Please:”))' - 你每次都通过循环重新分配它。 'numStudents'也具有相同的分配问题。这对于学习[debug python](https://pymotw.com/2/pdb/)来说也是一个很好的时间,因为从一眼就可以看出有多个逻辑错误,最终会给你提供不正确的结果。 – birryree
你指定'赢家=学生',所以看看学生的价值,它被分配'学生=(输入(“输入学生姓名请:”))''你从来没有真正分配正确的学生导致逻辑错误。 – kyle
这里可能有一个很好的问题,但你的问题标题是不恰当的。请查看http://stackoverflow.com/help/how-to-ask。 –