请优化此代码,实在不会了

img


此代码已绞尽我的脑汁,修改多次并无显著成效,总有某一处有问题

原代码少了几行,补上就行了,然后格式用制表符调整:

studentid = {}
studentscore = {}
course = set()
while True:
    line = input()
    if line == 'END':
        break
    words = line.split()
    if words[-1].isnumeric():
        if int(words[-1])>100 or int(words[-1])<0:
            print('成绩无效,请重新输入')
            continue
        score = studentscore.get(words[0])
        if score == None:
            score = {}
        score[words[1]] = words[2]
        studentscore[words[0]] = score
        course.add(words[1])
    else:
        if len(words[0])!= 11:
            print('学号无效,请重新输入')
            continue
        studentid[words[0]] = words[1]
coursename = list(course)
print('学号\t姓名',end='')
for name in coursename:
    print('\t'+name,end='')
print('\t平均分')
for id in studentid.keys():
    print(id+'\t'+studentid[id],end='')
    score = studentscore[studentid[id]]
    sum = 0
    cnt = 0
    for name in coursename:
        print('\t',end='')
        if name in score:
            print(score[name],end='')
            sum += int(score[name])
            cnt += 1
    print('\t'+str(int(sum/cnt)))

输出:

张三 语文 101
成绩无效,请重新输入
张三 语文 100
1001 张三
学号无效,请重新输入
10000000001 张三
张三 数学 95
李四 数学 98
李四 语文 90
10000000002 李四
END
学号            姓名    数学    语文    平均分
10000000001     张三    95      100     97
10000000002     李四    98      90      94

贴代码,不是贴截图。

建议这边将代码贴出来,一般截图没办法让别人看你原先代码怎么样的。

如有问题及时沟通

代码发出来呀