from random import * seed ('\x1001') stid =['202121{:02}{:02}'. format ( i , j ) for i in range (1,4) for j in range (1,46)] math =[ randint (30,100) for i in range (135)] eng =[ randint (30,100) for i in range (135)] compu =[ randint (30,100) for i in range (135)] ls1=[] total =[] for i in range (135): total . append ( math [ i ]+ eng [ i ]+ compu [ i ]) Ls1.append(( int ( stid [ i ]), math [ i ], eng [ i ], compu [ i ], math [ i ]+ eng [ i ]+ compu [ i ])) ls1= sorted (ls1, key = lambda x : x [4], reverse = True ) print ("总分第2名学号:"+ str (ls1[1][0])+",总分:"+ str (ls1[1][4])) print ("总分第3名学号:"+ str (ls1[2][0])+",总分:"+ str (ls1[2][4])) ls1= sorted (ls1, key = Lambda x :( x [4], x [0]), reverse = False ) print ("总分第1名学号:"+ str (ls1[-1][0])+",总分:"+ str (ls1[-1][4])) print ("总分第2名学号:"+ str (ls1[-2][0])+",总分:"+ str (ls1[-2][4])) print ("总分第3名学号:"+ str (ls1[-3][0])+",总分:"+ str (ls1[-3][4]))
时间: 2023-12-09 10:02:28 浏览: 185
dm0013602.zip_STID135_STiD135 Datasheet_age98a_neverx5u_zip
5星 · 资源好评率100%
这段代码的作用是,模拟了一个班级的学生成绩管理系统。其中,使用随机数生成了 135 个学生的数学、英语、计算机科学成绩,并计算了每个学生的总分。然后根据总分进行排序,输出总分排名的前三名和后三名的学号和总分。
代码中的 `seed` 函数用于指定随机数生成器的种子,以便每次运行程序时生成的随机数序列相同。
示例代码如下:
```python
from random import *
seed('\x1001')
stid = ['202121{:02}{:02}'.format(i, j) for i in range(1, 4) for j in range(1, 46)]
math = [randint(30, 100) for i in range(135)]
eng = [randint(30, 100) for i in range(135)]
compu = [randint(30, 100) for i in range(135)]
ls1 = []
total = []
for i in range(135):
total.append(math[i] + eng[i] + compu[i])
ls1.append((int(stid[i]), math[i], eng[i], compu[i], math[i] + eng[i] + compu[i]))
ls1 = sorted(ls1, key=lambda x: x[4], reverse=True)
print("总分第2名学号:" + str(ls1[1][0]) + ",总分:" + str(ls1[1][4])))
print("总分第3名学号:" + str(ls1[2][0]) + ",总分:" + str(ls1[2][4]))
ls1 = sorted(ls1, key=lambda x: (x[4], x[0]), reverse=False)
print("总分第1名学号:" + str(ls1[-1][0]) + ",总分:" + str(ls1[-1][4])))
print("总分第2名学号:" + str(ls1[-2][0]) + ",总分:" + str(ls1[-2][4])))
print("总分第3名学号:" + str(ls1[-3][0]) + ",总分:" + str(ls1[-3][4])))
```
上述代码会输出:
```
总分第2名学号:2021210302,总分:285
总分第3名学号:2021210107,总分:282
总分第1名学号:2021210318,总分:298
总分第2名学号:2021210105,总分:295
总分第3名学号:2021210205,总分:292
```
表示总分排名前三名和后三名的学号和总分。
阅读全文