大学学python,一脸懵逼,求编写的程序
现有4个列表,分别存放着学生的学号、语文成绩、数学成绩、英语成绩。内容分别为:list1=[1,2,3,4],list2=[98,67,89,80],list3=[100,90,83,62],list4=[90,98,100,82],要求用户输入学号,返回该学生的各门成绩及总分。如:用户输入的学号为3,则输出结果为:该学生成绩为语文89,数学83,英语100,总分272。
list1=[1,2,3,4] list2=[98,67,89,80] list3=[100,90,83,62] list4=[90,98,100,82] loop=True while loop: i=int(input("请输入学号,输入0退出:")) if i==0: break elif i in list1: print("该学生成绩为语文",list2[list1.index(i)],",数学",list3[list1.index(i)],",英语",list4[list1.index(i)],",总分",list2[list1.index(i)]+list3[list1.index(i)]+list4[list1.index(i)]) else: print("该学号不存在")
[此贴子已经被作者于2020-11-13 09:45编辑过]
class scores: def __init__(self, mandarin, math, english): self.s1 = mandarin self.s2 = math self.s3 = english def display(self): print('該學生成績為語文', self.s1, '數學', self.s2, '英文', self.s3, '總分', int(self.s1+self.s2+self.s3)) list1 = [1,2,3,4] list2 = [98,67,89,80] list3 = [100,90,83,62] list4 = [90,98,100,82] students = [] for i in range(4): students.append(scores(list2[i], list3[i], list4[i])) while True: num = int(input('請輸入學生號,輸入0退出: ')) if num == 0: break num -= 1 try: students[num].display() except: print('該學生不存在')
class Student: def __init__(self, results = {}): self.results = results def output(self): print('該學生成績為語文%d,數學%d,英語%d,總分%d'%(self.results['Mandarin'], self.results['Math'], self.results['English'], (self.results['Mandarin']+self.results['Math']+self.results['English']))) list1=[1, 2, 3, 4] list2=[98, 67, 89, 80] list3=[100, 90, 83, 62] list4=[90, 98, 100, 82] ID = {1: None, 2: None, 3: None, 4: None} for i in range(4): ID[i+1] = Student() ID[i+1].results['Mandarin'] = list2[i] ID[i+1].results['Math'] = list3[i] ID[i+1].results['English'] = list4[i] ID[2].output()
該學生成績為語文80,數學62,英語82,總分224