实验四

6.实验任务6:综合应用(1)——分数文件处理

with open('data6_1.txt','r',encoding='utf-8') as f:
    a=[]
    for line in f.readlines():
        m=line.strip().split()
        a.append(m)
a.sort(key=lambda l:l[2],reverse=True)

with open('data6_2.txt','w+',encoding='utf-8') as n:
        for i in a:
            n.write('  '.join(i)+'\n')

实验四实验四

 

7.实验任务7:综合应用(2)——随机抽点

import random

def get_lucky_boy(lis:list,trys:int):
    result=[]
    for i in range(trys):
        boy=random.choice(lis)
        lis.remove(boy)
        result.append(boy)
    a=[]
    for i in result:
        a.append(' '.join(i)+'\n')
    return ''.join(a)
ls=[]
with open('data7_1.txt','r',encoding='utf-8') as f:
   for line in f.readlines():
       m=line.strip().split()
       ls.append(m)
n=int(input('输入随机抽点人数:'))
boys=get_lucky_boy(lis=ls,trys=n)
print(boys)
with open('lucky.txt','w',encoding='utf-8') as nf:
        nf.write(boys)

  实验四

上一篇:实验四


下一篇:python小游戏 雷方块小解密