成績排名程式
要創建一個成績排名程式,你需要先定義一些基本元素,例如學生的名字、成績等等。以下是一個簡單的Python程式範例,它會讀取學生的名字和成績,並根據成績進行排名:
students = []
def add_student(name, score):
student = {
'name': name,
'score': score
}
students.append(student)
def display_students():
for student in students:
print(f"{student['name']} - {student['score']}")
def calculate_rank(score):
rank = 1
for student in students:
if score > student['score']:
rank += 1
return rank
def display_rank(score):
rank = calculate_rank(score)
print(f"The rank of {score} is {rank}.")
def main():
add_student("Alice", 90)
add_student("Bob", 80)
add_student("Charlie", 70)
add_student("Dave", 60)
display_students()
display_rank(90)
display_rank(80)
display_rank(70)
display_rank(60)
if __name__ == "__main__":
main()
這個程式定義了幾個函數:
add_student(name, score)
:用來新增一個學生。display_students()
:用來顯示所有學生的名字和成績。calculate_rank(score)
:用來計算一個成績的排名。display_rank(score)
:用來顯示一個成績的排名。main()
:主函數,用來初始化學生資料並顯示排名。
當你執行這個程式時,它會新增四個學生,並根據他們的成績進行排名。這個程式假設成績越高排名越前面。如果你想要根據其他標準進行排名,例如平均分數或最高分數,你可以修改calculate_rank
函數來滿足你的需求。