以下为举例代码:
# encoding:utf-8 # Python3.6 classmates = ['刘达', '王尔', '李珊', '陈思', '张悟'] courses = ['高等数学', 'Python程序设计', '宏观经bai济学', '管理学原理'] grade1 = {'刘达': 89, '王尔': 95, '李珊': 67, '陈思': 75} grade2 = {'刘达': 75, '王尔': 79, '李珊': 79} grade3 = {'李珊': 87, '陈思': 91, '张悟': 75} grade4 = {'刘达': 89, '王尔': 86, '张悟': 99} # 格式化字符串表示,具体内容可以搜索 python formate print("{:<6}{:^10}{:^10}{:^10}{:^10}".format("姓名\\科目", courses[0], courses[1], courses[2], courses[3])) for i, name in enumerate(classmates): g1, g2, g3, g4 = "", "", "", "" if name in grade1: g1 = grade1[name] if name in grade2: g2 = grade2[name] if name in grade3: g3 = grade3[name] if name in grade4: g4 = grade4[name] print("{:^6}{:^18}{:^10}{:^18}{:^18}".format(name, g1, g2, g3, g4))
下面是输出结果:
示例不是很难,当然代码那部分有一点小复杂,不过也是比较偏向于基础的知识了。想学习更多有趣的东西,一定要跟上小编的文章,让我们一起进阶python。