#C:\Users\stu\Desktop\Python\各章实验内容答案与素材参考\ch5\文本文件
f = list(open('C:\\Users\\stu\\Desktop\\Python\\各章实验内容答案与素材参考\\ch5\\文本文件\\students.txt','r'))
print(f)
one = []
for term in f:
one.append(term.split())
print(one)
for people in one:
for num in range(1,len(people)):
people[num] = int(people[num])
for people in one:
if ((sum(people[1:3+1])>=255) and (people[1]>=80)and (people[2]>=80)and (people[3]>=80)):
score = '优秀'
elif ((sum(people[1:3+1])>=180) and (people[2]+people[3]>=60)):
score = '合格'
elif ((sum(people[1:3+1]<180)or(people[2]<60)or(people[3]<60)):
score = '不合格'
print((people[0],'\t',score))
#C:\Users\stu\Desktop\Python\各章实验内容答案与素材参考\ch5\文本文件
f = list(open('C:\\Users\\stu\\Desktop\\Python\\各章实验内容答案与素材参考\\ch5\\文本文件\\students.txt','r'))
print(f)
n = open('C:\\Users\\stu\\Desktop\\crea.txt','r')
one = []
for term in f:
one.append(term.split())
print(one)
for people in one:
for num in range(1,len(people)):
people[num] = int(people[num])
for people in one:
if ((sum(people[1:3+1])>=255) and (people[1]>=80)and (people[2]>=80)and (people[3]>=80)):
score = '优秀'
elif ((sum(people[1:3+1])>=180) and (people[2]+people[3]>=60)):
score = '合格'
else:
score = '不合格'
print((people[0],'\t',score))
n = open('C:\\Users\\stu\\Desktop\\crea.txt','w')
n.write((people[0],'\t',score)
f.close()
n.close()
#C:\Users\stu\Desktop\Python\各章实验内容答案与素材参考\ch5\文本文件
f = list(open('C:\\Users\\stu\\Desktop\\Python\\各章实验内容答案与素材参考\\ch5\\文本文件\\students.txt','r'))
print(f)
one = []
whole = ''
for term in f:
one.append(term.split())
print(one)
for people in one:
for num in range(1,len(people)):
people[num] = int(people[num])
for people in one:
if ((sum(people[1:3+1])>=255) and (people[1]>=80)and (people[2]>=80)and (people[3]>=80)):
score = '优秀'
elif ((sum(people[1:3+1])>=180) and (people[2]+people[3]>=60)):
score = '合格'
else:
score = '不合格'
sing = people[0]+'\t'+score+'\n'
whole += sing
print(whole)
n = open('D:\\c.txt','w')
n.write(whole)
n.close()
无论如何以最可怕的方式完成了这道题。。。。
#C:\Users\stu\Desktop\Python\各章实验内容答案与素材参考\ch5\文本文件
f = open('C:\\Users\\stu\\Desktop\\Python\\各章实验内容答案与素材参考\\ch5\\文本文件\\students.txt','r')
n = open('D:\\t.txt','w')
s = ''
d = {1:'优秀',2:'合格',3:'不合格'}
while True:
x = f.readline()
if x == '': #如果读到的是空字符串,即如果读完了,那就停止。
break
peolist = x.split()#去掉所有的白空格。先转化成列表才能对字符串进行修改。
s = s + peolist[0]+'\t' #学号和制表符连接
for i in range(1,3+1):
peolist[i] = int(peolist[i])#成绩转化为整型
sumnum = sum(peolist[1:3+1])
if peolist[1]>= 80 and peolist[2]>=80 and peolist[3]>=80 and sumnum >= 255:
key = 1
elif peolist[2]>=60 and peolist[3]>=60 and sumnum >= 180:
key = 2
elif peolist[2]<60 or peolist[3]<60 or sumnum <180:
key = 3
s = s+d[key]+'\n'
n.write(s)
f.close()
n.close()