练习1
【需求】输入一个分数。分数在0-100之间。90以上是A,80以上是B,70以上是C,60以上是D,60以下是E。
方法1、
score = int(input("请输入分数:")) grad = "" if score >100 or score <0: score = int(input('请输入分数(0-100):')) elif score >= 90: grad = 'A' elif score >= 80: grad = "B" elif score >= 70: grad = "C" elif score >= 60: grad = "D" else: grad = "E" print("成绩是{0},等级为{1}".format(score,grad))
方法2:
score = int(input("请输入分数:")) grade = "ABCDE" num = 0 if score >100 or score < 0: score = int(input("请输入分数(0-100):")) else: num = score // 10 if num < 6: num = 5 print('分数为{0},等级为{1}'.format(score,grade[9-num]))
【需求】用列表和字典存储下表信息,并打印出表中工资高于30000的数据。
d1 = dict(name='勒布朗',age=35,salary=35000,city="洛杉矶") d2 = dict(name='凯文',age=30,salary=30000,city="布鲁克林") d3 = dict(name='巴特勒',age=32,salary=20000,city="迈阿密") d4 = dict(name='欧文',age=28,salary=18000,city="鲁克林") d5 = dict(name='戴维斯',age=29,salary=33000,city="洛杉矶") l = [d1,d2,d3,d4,d5] for i in l: if i.get('salary')>30000: print(i)