Skip to content

Python 607 成績計算

Python TQC

題目說明:

請撰寫一程式,讓使用者輸入三位學生各五筆成績,接著再計算並輸出每位學生的總分及平均分數。

Info

平均分數輸出到小數點後第二位。

輸入與輸出會交雜如下,輸出的部份以粗體字表示

The 1st student:
78
89
88
70
60
The 2nd student:
90
78
66
68
78
The 3rd student:
69
97
70
89
90
Student 1
#Sum 385
#Average 77.00
Student 2
#Sum 380
#Average 76.00
Student 3
#Sum 415
#Average 83.00


題目解析

問題分析:

這個問題要求我們讓使用者輸入三位學生各五筆成績,然後計算每位學生的總分和平均分數。

解題思路:

  1. 使用一個 for 迴圈來提示使用者輸入每位學生的成績。
  2. 在迴圈中,使用另一個 for 迴圈來輸入每位學生的五筆成績,並計算總分。
  3. 計算每位學生的平均分數,並輸出結果。

思考方向:

  1. 如何使用迴圈來提示使用者輸入成績?
  2. 如何在程式中組織和計算學生的成績?
  3. 如何輸出每位學生的總分和平均分數?
Solution
ID = ["1st", "2nd", "3rd"]
def compute(x):
  SUM = [0]*x
  for i in range(x):
    print("The {} student:".format(ID[i]))
    for j in range(5):
      SUM[i] += eval(input())
  return SUM

l = compute(3)
for i in range(3):
  print("Student %d"%(i+1))
  print("#Sum %d"%l[i])
  print("#Average %.2f"%(l[i]/5))
# 建立三個空列表來儲存每位學生的成績
student1_scores = []
student2_scores = []
student3_scores = []

# 輸入第一位學生的五筆成績
print("The 1st student:")
for i in range(5):
    score = int(input())
    student1_scores.append(score)

# 輸入第二位學生的五筆成績
print("The 2nd student:")
for i in range(5):
    score = int(input())
    student2_scores.append(score)

# 輸入第三位學生的五筆成績
print("The 3rd student:")
for i in range(5):
    score = int(input())
    student3_scores.append(score)

# 計算每位學生的總分和平均分數
student1_sum = sum(student1_scores)
student1_avg = student1_sum / 5

student2_sum = sum(student2_scores)
student2_avg = student2_sum / 5

student3_sum = sum(student3_scores)
student3_avg = student3_sum / 5

# 輸出每位學生的總分和平均分數
print("Student 1")
print(f"#Sum {student1_sum}")
print("#Average {:.2f}".format(student1_avg))

print("Student 2")
print(f"#Sum {student2_sum}")
print("#Average {:.2f}".format(student2_avg))


print("Student 3")
print(f"#Sum {student3_sum}")
print("#Average {:.2f}".format(student3_avg))

注意

注意印出的號碼


Last update : 13 novembre 2024
Created : 13 novembre 2024

Comments

Comments