我想讓我的程式從任何數量的學生那里獲得任何數量的分數,并對它們進行計算。
student_number = 1
try。
score = int(input("請輸入學生" str(student_number) " 的分數(-1。退出)。) "))
except。
print("The score entered is not a number. 請重新輸入。")
score = int(input("請輸入學生" str(student_number) " 的分數(-1: 退出)。) "))
while score != -1:
try:
score = int(input("請輸入學生" str(student_number) " 的分數(-1: 退出)。) "))
except。
print("The score entered is not a number. 請重新輸入。")
score = int(input("請輸入學生" str(student_number) " 的分數(-1: 退出)。) "))
more_student = input("還有沒有學生?(是或否)。")
while more_student == "Yes"/span>:
student_number = student_number 1.
try:
score = int(input("請輸入學生" str(student_number) " 的分數(-1。退出)。) "))
except。
print("The score entered is not a number. 請重新輸入。")
score = int(input("請輸入學生" str(student_number) " 的分數(-1: 退出)。) "))
while score != -1:
score = int(input("請輸入學生" str(student_number) " 的分數(-1: 退出)。) "))
more_student = input("還有沒有學生?(是或否)。")
print(" done")
代替print("done"),我想以某種方式把我收到的每一個輸入,并能按學生進行分割,例如輸出:
學生_有4個分數。他們的平均分數是_。 學生_有3個分數。他們的平均分是_。
uj5u.com熱心網友回復:
我想我會使用一個字典。這將允許收集每個學生的資料,即。
scores = {}.
student = input("Student ?") # Ask for student。
if(student not in scores.keys()) 。# 必要時創建新的學生。
scores[student] = []
score = int(input("Score ? ") # generate & store score for the student.
scores[student].append(score)
然后為每個學生計算分數的平均值 ... 這么多的可能性。對我來說,最簡單的是:
for student, score_list in scores.items() 。
nb_scores = len(score_list)
mean_score = sum(score_list)/len(score_list)
print("Student {} had {} scores, with a mean of {}"/span>. format(student, nb_scores, mean_score))
uj5u.com熱心網友回復:
我包含了兩個無限回圈,當輸入-1的時候就會中斷。使用了一個hashmap,每個學生ID都是一個鍵,并以一個空陣列初始化,在第二個回圈執行時存盤分數。
marks = {}。
while True:
i = int(input("輸入學生ID或按-1退出")
if i == -1:
break: break.
else:
marks[i] = []
while True:
x = int(input("Enter Mark or press -1 to exit"/span>)
if x! =-1:
marks[i].append(x)
else:
break: 爆發。
for i in marks.keys() 。
count = len(mark[i])
avg = sum(mark[i])/count
print("學生{}有{}個得分。他們的平均分是{}".format(i, count, avg)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/320007.html
標籤:
