私は以下の問題があり、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(" Student Nameを入力してください: "))' - あなたはループを介して毎回それを再割り当てしています。 'numStudents'と同じ割り当て問題があります。これはまた、[pythonのデバッグ](https://pymotw.com/2/pdb/)を学ぶのに良い時間です。間違った結果をもたらす複数のロジックエラーが一目瞭然です。 – birryree
あなたは 'winner = students'を割り当てていますので、学生の値を見て、' students =(input( "Student nameを入力してください:")) '論理的なエラーの結果として生徒を実際に割り当てません。 – kyle
ここには良い質問があるかもしれませんが、あなたの質問のタイトルは不適切です。 http://stackoverflow.com/help/how-to-askをご覧ください。 –