我給出了以下問題,並要求使用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。 –