实验任务1
实验源码
1 import random 2 3 print('用列表存储随机整数:') 4 lst = [random.randint(0, 100) for i in range(5)] 5 print(lst) 6 7 print('\n用集合存储随机整数:') 8 s1 = {random.randint(0, 100) for i in range(5)} 9 print(s1) 10 11 print('\n用集合存储随机整数:') 12 s2 = set() 13 while len(s2) < 5: 14 s2.add(random.randint(0, 100)) 15 print(s2)
运行测试截图
实验结论
1. random.randint(0, 100)生成的随机整数范围是【0, 100】,能取到100。
2. 利用list(range(5))生成的有序序列范围是【0, 1, 2, 3, 4】,不包括5。
利用list(range(1, 5))生成的有序序列范围是【1, 2, 3, 4】,不包括5。
3. 使用line8代码生成的集合s1,len(s1)不一定是5,因为集合中相同元素会被过滤掉。
4. 使用line12-14生成的集合s2,len(s2)一定是5,while语句保证了其元素个数一定是5。
实验任务2
task2-1
实验源码
1 # 字典遍历 2 book_info = {'isbn': '978-7-5356-8297-0', 3 '书名': '白鲸记', 4 '作者': '克里斯多夫.夏布特', 5 '译者': '高文婧', 6 '出版社': '湖南美术出版社', 7 '售价': 82 8 } 9 10 # 遍历key-value对:实现方式1 11 for key, value in book_info.items(): 12 print(f'{key}: {value}') 13 print() 14 15 # 遍历key-value对:实现方式2 16 for item in book_info.items(): 17 print(f'{item[0]}: {item[1]}') 18 print() 19 20 # 遍历值:实现方式1 21 for value in book_info.values(): 22 print(value, end = ' ') 23 print() 24 25 # 遍历值: 实现方式2 26 for key in book_info.keys(): 27 print(book_info[key], end = ' ')
运行测试截图
task2-2
实验源码
1 # 字典遍历 2 book_info = {'isbn': '978-7-5356-8297-0', 3 '书名': '白鲸记', 4 '作者': '克里斯多夫.夏布特', 5 '译者': '高文婧', 6 '出版社': '湖南美术出版社', 7 '售价': 82 8 } 9 10 # 遍历key-value对: 实现方式1 11 for key, value in book_info.items(): 12 print(f'{key}: {value}') 13 print() 14 15 # 遍历key-value对: 实现方式2 16 for item in book_info.items(): 17 print(f'{item[0]}: {item[1]}') 18 print() 19 20 # 遍历值:实现方式1 21 for value in book_info.values(): 22 print(value, end = ' ') 23 print() 24 25 # 遍历值: 实现方式2 26 for key in book_info.keys(): 27 print(book_info[key], end = ' ')
运行测试截图
task2-3
实验源码
1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, 2 {'书名': '局外人', '作者': '阿尔贝.加缪'}, 3 {'书名': '设计中的设计', '作者': '原研哉'}, 4 {'书名': '万历十五年', '作者': '黄仁宇'}, 5 {'书名': '刀锋', '作者': '毛姆'} 6 ] 7 n = 1 8 for i in book_infos: 9 print(n, '.''《'+i['书名']+'》,', i['作者']) 10 n = n+1
运行测试截图
实验任务3
实验源码
1 text = '''The Zen of Python, by Tim Peters 2 3 Beautiful is better than ugly. 4 Explicit is better than implicit. 5 Simple is better than complex. 6 Complex is better than complicated. 7 Flat is better than nested. 8 Sparse is better than dense. 9 Readability counts. 10 Special cases aren't special enough to break the rules. 11 Although practicality beats purity. 12 Errors should never pass silently. 13 Unless explicitly silenced. 14 In the face of ambiguity, refuse the temptation to guess. 15 There should be one-- and preferably only one --obvious way to do it. 16 Although that way may not be obvious at first unless you're Dutch. 17 Now is better than never. 18 Although never is often better than *right* now. 19 If the implementation is hard to explain, it's a bad idea. 20 If the implementation is easy to explain, it may be a good idea. 21 Namespaces are one honking great idea -- let's do more of those!''' 22 23 list1 = list(text.lower().split()) 24 d1 = dict.fromkeys('abcdefghijklmnopqrstuvwxyz', 0) 25 for i in range(len(list1)): 26 for j in list1[i]: 27 if ord(j) in range(97, 97+26): 28 d1[j] += 1 29 else: 30 continue 31 d2 = sorted([(x[1], x[0]) for x in d1.items()], reverse = True) 32 d3 = [(x[1], x[0]) for x in d2] 33 for i in range(len(d3)): 34 print(f'{d3[i][0]}:{d3[i][1]}')
运行测试截图
实验任务4
实验源码
1 code_majors = {8326: '地信类', 2 8329: '计算机类', 3 8330: '气科类', 4 8336: '防灾工程', 5 8345: '海洋科学', 6 8382: '气象工程'} 7 print('{0:-^50}'.format('专业代号信息')) 8 for key, value in code_majors.items(): 9 print(f'{key}:{value}') 10 print('{0:-^50}'.format('学生专业查询')) 11 while True: 12 x = input('请输入学号:') 13 if x == '#': 14 print('查询结束...') 15 break 16 else: 17 y = eval(x[4:8]) 18 if y in list(code_majors.keys()): 19 print(f'专业是:{code_majors[y]}') 20 else: 21 print('不在这些专业中...')
运行测试截图
实验任务5
实验源码
1 print("猜猜2020年哪一天会是你的lucky day:-") 2 import random 3 lucky_day=random.randint(1, 31) 4 x = int(lucky_day) 5 x1 = int(input("你有三次机会,猜吧(1~31):")) 6 if x1 == x: 7 print("哇,猜中了") 8 elif x1 < x: 9 print("猜早啦,你的lucky day还没到呢") 10 x2 = int(input("再猜(1~31):")) 11 if x2 == x: 12 print("哇,猜中了") 13 elif x2 < x: 14 print("猜早啦,你的lucky day还没到呢") 15 x3 = int(input("再猜(1~31):")) 16 if x3 == x: 17 print("哇,猜中了") 18 elif x3 < x: 19 print("猜早啦,你的lucky day还没到呢") 20 print("次数用光啦") 21 print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-") 22 elif x3 > x: 23 print("猜晚啦,你的lucky day已经过了") 24 print("次数用光啦") 25 print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-") 26 elif x2 > x: 27 print("猜晚啦,你的lucky day已经过了") 28 x3 = int(input("再猜(1~31):")) 29 if x3 == x: 30 print("哇,猜中了") 31 elif x3 < x: 32 print("猜早啦,你的lucky day还没到呢") 33 print("次数用光啦") 34 print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-") 35 elif x3 > x: 36 print("猜晚啦,你的lucky day已经过了") 37 print("次数用光啦") 38 print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-") 39 elif x1 > x: 40 print("猜晚啦,你的lucky day已经过了") 41 x2 = int(input("再猜(1~31):")) 42 if x2 == x: 43 print("哇,猜中了") 44 elif x2 < x: 45 print("猜早啦,你的lucky day还没到呢") 46 x3 = int(input("再猜(1~31):")) 47 if x3 == x: 48 print("哇,猜中了") 49 elif x3 < x: 50 print("猜早啦,你的lucky day还没到呢") 51 print("次数用光啦") 52 print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-") 53 elif x3 > x: 54 print("猜晚啦,你的lucky day已经过了") 55 print("次数用光啦") 56 print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-") 57 elif x2 > x: 58 print("猜晚啦,你的lucky day已经过了") 59 x3 = int(input("再猜(1~31):")) 60 if x3 == x: 61 print("哇,猜中了") 62 elif x3 < x: 63 print("猜早啦,你的lucky day还没到呢") 64 print("次数用光啦") 65 print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-") 66 elif x3 > x: 67 print("猜晚啦,你的lucky day已经过了") 68 print("次数用光啦") 69 print("偷偷告诉你,5月你的lucky day是",x,"号.good lucky:-")
运行测试截图
实验任务6
实验源码
1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], 2 '2049777002': ['音乐', '旅行'], 3 '2049777003': ['马拉松', '健身', '游戏'], 4 '2049777004': [], 5 '2049777005': ['足球', '阅读'], 6 '2049777006': ['发呆', '闲逛'], 7 '2049777007': [], 8 '2049777008': ['书法', '电影'], 9 '2049777009': ['音乐', '阅读', '电影', '漫画'], 10 '2049777010': ['数学', '推理', '音乐', '旅行']} 11 v = {} 12 for i in datas.values(): 13 for j in i: 14 v[j] = v.get(j, 0)+1 15 x = list(v.keys()) 16 y = list(v.values()) 17 z = [(y[i], x[i]) for i in range(len(x))] 18 for j in sorted(z, reverse=True): 19 print(f"{j[1]}:{j[0]}")
运行测试截图
实验任务7
task7-1
实验源码
1 print('欢迎使用家用电器销售系统!') 2 pro = [['0001', '电视机', '海尔', 5999.00, 20], 3 ['0002', '冰箱', '西门子', 6998.00, 15], 4 ['0003', '洗衣机', '小天鹅', 1999.00, 10], 5 ['0004', '空调', '格力', 3900.00, 0], 6 ['0005', '热水器', '格力', 688.00, 30], 7 ['0006', '笔记本', '联想', 5699.00, 10], 8 ['0007', '微波炉', '苏泊尔', 480.00, 33], 9 ['0008', '投影仪', '松下', 1250.00, 12], 10 ['0009', '吸尘器', '飞利浦', 999.00, 9]] 11 products_cart = [] 12 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 13 while option in ['1', '2', '3', ]: 14 if option == '1': 15 print('产品和价格信息如下') 16 print('***********************************************************') 17 print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % '价格', '%-10s' % '库存数量') 18 print('-----------------------------------------------------------') 19 for i in range(len(pro)): 20 print('%-10s' % pro[i][0], '%-10s' % pro[i][1], '%-10s' % pro[i][2], '%10.2f' % pro[i][3], 21 '%10d' % pro[i][4]) 22 23 print('-----------------------------------------------------------') 24 elif option == '2': 25 pro_id = input('请输入您要购买的产品编号:') 26 while pro_id not in [item[0] for item in pro]: 27 pro_id = input('编号不存在,请重新输入您要购买的产品编号:') 28 count = int(input('请输入您要购买的产品数量:')) 29 while count > pro[int(pro_id) - 1][4]: 30 count = input('数量超出库存。请重新输入您要购买的产品数量:') 31 if pro_id not in [item[0] for item in products_cart]: 32 products_cart.append([pro_id, count]) 33 else: 34 for i in range(len(products_cart)): 35 if products_cart[i][0] == pro_id: 36 products_cart[i][1] += count 37 else: 38 print('购物车信息如下:') 39 print('************************************') 40 print('%-10s' % '编号', '%-10s' % '购买数量') 41 print('------------------------------------') 42 for i in range(len(products_cart)): 43 print('%-10s' % products_cart[i][0], '%6d' % products_cart[i][1]) 44 print('------------------------------------') 45 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。') 46 if len(products_cart) > 0: 47 amount = 0 48 for i in range(len(products_cart)): 49 pro_index = 0 50 for j in range(len(pro)): 51 if pro[j][0] == products_cart[i][0]: 52 pro_index = j 53 break 54 price = pro[pro_index][3] 55 count = products_cart[i][1] 56 amount += price * count 57 if 5000 < amount <= 10000: 58 amount = amount * 0.95 59 elif 10000 < amount <= 20000: 60 amount = amount * 0.9 61 elif 20000 < amount: 62 amount = amount * 0.85 63 else: 64 amount = amount * 1 65 print('购买成功,您需要支付%8.2f元' % amount) 66 print('谢谢您的光临,下次再见!')
运行测试截图
task7-2
实验源码
1 print('欢迎使用家用电器销售系统!') 2 pro = [['0001', '电视机', '海尔', 5999.00, 20], 3 ['0002', '冰箱', '西门子', 6998.00, 15], 4 ['0003', '洗衣机', '小天鹅', 1999.00, 10], 5 ['0004', '空调', '格力', 3900.00, 0], 6 ['0005', '热水器', '格力', 688.00, 30], 7 ['0006', '笔记本', '联想', 5699.00, 10], 8 ['0007', '微波炉', '苏泊尔', 480.00, 33], 9 ['0008', '投影仪', '松下', 1250.00, 12], 10 ['0009', '吸尘器', '飞利浦', 999.00, 9]] 11 products_cart = [] 12 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 13 while option in ['1', '2', '3', ]: 14 if option == '1': 15 print('产品和价格信息如下') 16 print('***********************************************************') 17 print('{:<10} {:<10} {:<10} {:<10} {:<10}'.format('编号', '名称', '品牌', '价格', '库存数量')) 18 print('-----------------------------------------------------------') 19 for i in range(len(pro)): 20 print('{:<10}'.format(pro[i][0]), '{:<10}'.format(pro[i][1]), '{:<10}'.format(pro[i][2]), 21 '{:<10.2f}'.format(pro[i][3]), 22 '{:>10}'.format(pro[i][4])) 23 24 print('-----------------------------------------------------------') 25 elif option == '2': 26 pro_id = input('请输入您要购买的产品编号:') 27 while pro_id not in [item[0] for item in pro]: 28 pro_id = input('编号不存在,请重新输入您要购买的产品编号:') 29 count = int(input('请输入您要购买的产品数量:')) 30 while count > pro[int(pro_id) - 1][4]: 31 count = input('数量超出库存。请重新输入您要购买的产品数量:') 32 if pro_id not in [item[0] for item in products_cart]: 33 products_cart.append([pro_id, count]) 34 else: 35 for i in range(len(products_cart)): 36 if products_cart[i][0] == pro_id: 37 products_cart[i][1] += count 38 else: 39 print('购物车信息如下:') 40 print('************************************') 41 print('{:<10} {:<10} '.format('编号', '购买数量')) 42 print('------------------------------------') 43 for i in range(len(products_cart)): 44 print('{:10s}'.format(products_cart[i][0]), '{:6d}'.format(products_cart[i][1])) 45 print('------------------------------------') 46 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。') 47 if len(products_cart) > 0: 48 amount = 0 49 for i in range(len(products_cart)): 50 pro_index = 0 51 for j in range(len(pro)): 52 if pro[j][0] == products_cart[i][0]: 53 pro_index = j 54 break 55 price = pro[pro_index][3] 56 count = products_cart[i][1] 57 amount += price * count 58 if 5000 < amount <= 10000: 59 amount = amount * 0.95 60 elif 10000 < amount <= 20000: 61 amount = amount * 0.9 62 elif 20000 < amount: 63 amount = amount * 0.85 64 else: 65 amount = amount * 1 66 print('购买成功您需要支付{:8.2f}元'.format(amount)) 67 print('谢谢您的光临,下次再见!')
运行测试截图
实验任务8
task8-1
实验源码
1 print('欢迎使用家用电器销售系统!') 2 pro = [{'id': '0001', 'name': '电视机', 'brand': '海尔', 'price': 5999.00, 'count': 20}, 3 {'id': '0002', 'name': '冰箱', 'brand': '西门子', 'price': 6998.00, 'count': 15}, 4 {'id': '0003', 'name': '洗衣机', 'brand': '小天鹅', 'price': 1999.00, 'count': 10}, 5 {'id': '0004', 'name': '空调', 'brand': '格力', 'price': 3900.00, 'count': 0}, 6 {'id': '0005', 'name': '热水器', 'brand': '格力', 'price': 688.00, 'count': 30}, 7 {'id': '0006', 'name': '笔记本', 'brand': '联想', 'price': 5699.00, 'count': 10}, 8 {'id': '0007', 'name': '微波炉', 'brand': '苏泊尔', 'price': 480.00, 'count': 33}, 9 {'id': '0008', 'name': '投影仪', 'brand': '松下', 'price': 1250.00, 'count': 12}, 10 {'id': '0009', 'name': '吸尘器', 'brand': '飞利浦', 'price': 999.00, 'count': 9} 11 ] 12 products_cart = [] 13 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 14 while option in ['1', '2', '3', ]: 15 if option == '1': 16 print('产品和价格信息如下') 17 print('***********************************************************') 18 print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % '价格', '%-10s' % '库存数量') 19 print('-----------------------------------------------------------') 20 for i in range(len(pro)): 21 print('%-10s' % pro[i]['id'], '%-10s' % pro[i]['name'], '%-10s' % pro[i]['brand'], 22 '%10.2f' % pro[i]['price'], '%10d' % pro[i]['count']) 23 print('-----------------------------------------------------------') 24 elif option == '2': 25 pro_id = input('请输入您要购买的产品编号:') 26 while pro_id not in [item['id'] for item in pro]: 27 pro_id = input('编号不存在,请重新输入您要购买的产品编号:') 28 count = int(input('请输入您要购买的产品数量:')) 29 while count > pro[int(pro_id) - 1]['count']: 30 count = input('数量超出库存。请重新输入您要购买的产品数量:') 31 if pro_id not in [item['id'] for item in products_cart]: 32 products_cart.append({'id': pro_id, 'count': count}) 33 else: 34 for i in range(len(products_cart)): 35 if products_cart[i].get('id') == pro_id: 36 products_cart[i]['count'] += count 37 38 for i in range(len(pro)): 39 if pro[i]['id'] == pro_id: 40 pro[i]['count'] -= count 41 else: 42 print('购物车信息如下:') 43 print('************************************') 44 print('%-10s' % '编号', '%-10s' % '购买数量') 45 print('------------------------------------') 46 for i in range(len(products_cart)): 47 print('%-10s' % products_cart[i]['id'], '%6d' % products_cart[i]['count']) 48 print('------------------------------------') 49 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。') 50 if len(products_cart) > 0: 51 amount = 0 52 for i in range(len(products_cart)): 53 pro_index = 0 54 for j in range(len(pro)): 55 if pro[j]['id'] == products_cart[i]['id']: 56 pro_index = j 57 break 58 price = pro[pro_index]['price'] 59 count = products_cart[i]['count'] 60 amount += price * count 61 if 5000 < amount <= 10000: 62 amount = amount * 0.95 63 elif 10000 < amount <= 20000: 64 amount = amount * 0.9 65 elif 20000 < amount: 66 amount = amount * 0.85 67 else: 68 amount = amount * 1 69 print('购买成功,您需要支付%8.2f元' % amount) 70 print('谢谢您的光临,下次再见!')
运行测试截图
task8-2
实验源码
1 print('欢迎使用家用电器销售系统!') 2 pro = [{'id': '0001', 'name': '电视机', 'brand': '海尔', 'price': 5999.00, 'count': 20}, 3 {'id': '0002', 'name': '冰箱', 'brand': '西门子', 'price': 6998.00, 'count': 15}, 4 {'id': '0003', 'name': '洗衣机', 'brand': '小天鹅', 'price': 1999.00, 'count': 10}, 5 {'id': '0004', 'name': '空调', 'brand': '格力', 'price': 3900.00, 'count': 0}, 6 {'id': '0005', 'name': '热水器', 'brand': '格力', 'price': 688.00, 'count': 30}, 7 {'id': '0006', 'name': '笔记本', 'brand': '联想', 'price': 5699.00, 'count': 10}, 8 {'id': '0007', 'name': '微波炉', 'brand': '苏泊尔', 'price': 480.00, 'count': 33}, 9 {'id': '0008', 'name': '投影仪', 'brand': '松下', 'price': 1250.00, 'count': 12}, 10 {'id': '0009', 'name': '吸尘器', 'brand': '飞利浦', 'price': 999.00, 'count': 9} 11 ] 12 products_cart = [] 13 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 14 while option in ['1', '2', '3', ]: 15 if option == '1': 16 print('产品和价格信息如下') 17 print('***********************************************************') 18 print('{:<10} {:<10} {:<10} {:<10} {:<10}'.format('编号', '名称', '品牌', '价格', '库存数量')) 19 print('-----------------------------------------------------------') 20 for i in range(len(pro)): 21 print('{:<10}'.format(pro[i]['id']), '{:<10}'.format(pro[i]['name']), '{:<10}'.format(pro[i]['brand']), 22 '{:<10.2f}'.format(pro[i]['price']), 23 '{:>10}'.format(pro[i]['count'])) 24 print('-----------------------------------------------------------') 25 elif option == '2': 26 pro_id = input('请输入您要购买的产品编号:') 27 while pro_id not in [item['id'] for item in pro]: 28 pro_id = input('编号不存在,请重新输入您要购买的产品编号:') 29 count = int(input('请输入您要购买的产品数量:')) 30 while count > pro[int(pro_id) - 1]['count']: 31 count = input('数量超出库存。请重新输入您要购买的产品数量:') 32 if pro_id not in [item['id'] for item in products_cart]: 33 products_cart.append({'id': pro_id, 'count': count}) 34 else: 35 for i in range(len(products_cart)): 36 if products_cart[i].get('id') == pro_id: 37 products_cart[i]['count'] += count 38 39 for i in range(len(pro)): 40 if pro[i]['id'] == pro_id: 41 pro[i]['count'] -= count 42 else: 43 print('购物车信息如下:') 44 print('************************************') 45 print('{:<10} {:<10} '.format('编号', '购买数量')) 46 print('------------------------------------') 47 for i in range(len(products_cart)): 48 print('{:10s}'.format(products_cart[i]['id']), '{:6d}'.format(products_cart[i]['count'])) 49 print('------------------------------------') 50 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。') 51 if len(products_cart) > 0: 52 amount = 0 53 for i in range(len(products_cart)): 54 pro_index = 0 55 for j in range(len(pro)): 56 if pro[j]['id'] == products_cart[i]['id']: 57 pro_index = j 58 break 59 price = pro[pro_index]['price'] 60 count = products_cart[i]['count'] 61 amount += price * count 62 if 5000 < amount <= 10000: 63 amount = amount * 0.95 64 elif 10000 < amount <= 20000: 65 amount = amount * 0.9 66 elif 20000 < amount: 67 amount = amount * 0.85 68 else: 69 amount = amount * 1 70 print('购买成功您需要支付{:8.2f}元'.format(amount)) 71 print('谢谢您的光临,下次再见!')
运行测试截图
标签:语句,count,pro,编程,数据类型,cart,products,print,id From: https://www.cnblogs.com/siyi5981/p/17332823.html