📜  Python程序,用于根据学生的分数降序排列

📅  最后修改于: 2022-05-13 01:54:59.549000             🧑  作者: Mango

Python程序,用于根据学生的分数降序排列

考虑一个有 20 名学生的班级,他们的名字和分数都给了你。任务是根据学生的分数按降序排列。编写一个Python程序来执行该任务。

例子:

Input:
Arun: 78%
Geeta: 86%
Shilpi: 65%

Output: 
Geeta: 86%
Arun: 78%
Shilpi: 65%


方法:由于问题表明我们需要首先收集学生姓名和他们的分数,因此我们将在列表数据结构中一个一个地获取输入,然后使用基于学生百分比的 sorted() 内置函数以相反的顺序对它们进行排序最后我们将相应地打印该值。

下面是实现:

Python3
print("-----Program for printing student name with marks using list-----")
  
# create an empty dictionary
D = {}
  
n = int(input('How many student record you want to store?? '))
  
# create an empty list
# Add student information to the list
ls = []
  
for i in range(0, n):
    
      # Take combined input name and 
    # percentage and split values 
    # using split function.
    x,y = input("Enter the student name and it's percentage: ").split()
      
    # Add name and marks stored in x, y
    # respectively using tuple to the list
    ls.append((y,x))
      
# sort the elements of list
# based on marks
ls = sorted(ls, reverse = True)
  
print('Sorted list of students according to their marks in descending order')
  
for i in ls:
    
    # print name and marks stored in 
    # second and first position 
    # respectively in list of tuples.
    print(i[1], i[0])


输出:

-----Program for printing student name with marks using list-----
How many student record you want to store?? 3
Enter the student name and percentage: Arun: 78%
Enter the student name and percentage: Geeta: 86%
Enter the student name and percentage: Shilpi: 65%
Sorted list of students according to their marks in descending order
Geeta: 86%
Arun: 78%
Shilpi: 65%