if elif else

#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()


书上的答案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值