task1:
实验源码:
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) 生成的随机整数范围是?能否取到100?
答:[0,100];可以
问题2:利用 list(range(5)) 生成的有序序列范围是?是否包括5?
答:[0,5);不包括
利用 list(range(1,5)) 生成的有序序列范围是?是否包括5?
答:[1,5);不包括
问题3:使用line8代码生成的集合s1,len(s1)一定是5吗?如果不是,请解释原因。
答:不一定。随机生成的数据可能重复,而集合中不允许有重复元素,相同数据只出现一次。
问题4:使用line12-14生成的集合s2,len(s2)一定是5吗?如果不是,请解释原因。
答:是。while语句中规定,当len(s2)达到5后才会跳出循环。
task2.1
实验源码:
1 # 列表遍历 2 lst = [55, 92, 88, 79, 96] 3 4 # 遍历方式1: 使用while + 索引 5 i = 0 6 while i < len(lst): 7 print(lst[i], end = ' ') 8 i += 1 9 10 print() 11 12 # 遍历方式2:使用for + 索引 13 for i in range(len(lst)): 14 print(lst[i], end = ' ') 15 print() 16 17 # 遍历方式3: 使用for...in 18 for i in lst: 19 print(i, end = ' ') 20 print()
运行截图:
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 lst1 = [] 8 for i in range(len(book_infos)): 9 for j in book_infos[i].values(): 10 lst1.append(j) 11 lst2 = [] 12 for m in range(0, len(lst1), 2): 13 lst2.append(lst1[m]) 14 15 lst3 = [] 16 for n in range(1, len(lst1), 2): 17 lst3.append(lst1[n]) 18 19 for x in range(len(lst2)): 20 print(f'{x+1}.《{lst2[x]}》,{lst3[x]}')
运行截图:
task3
实验源码:
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 24 text = text.lower() 25 lst1 = [] 26 for i in range(97, 97+26): 27 num = text.count(chr(i)) 28 lst1.append(num) 29 30 d = {} 31 for j in range(26): 32 d.update({chr(j+97): lst1[j]}) 33 34 lst2 = list(d.items()) 35 lst3 = [(v, k) for k, v in lst2] 36 37 for v, k in sorted(lst3, reverse=True): 38 print(f'{k}:{v}')
运行截图:
task4
实验源码:
1 code_majors = {'8326': '地信类', 2 '8329': '计算机类', 3 '8330': '气科类', 4 '8336': '防灾工程', 5 '8345': '海洋科学', 6 '8382': '气象工程' 7 } 8 9 title1 = '专业代号信息' 10 print(f'{title1:-^50}') 11 12 for key, value in code_majors.items(): 13 print(f'{key}:{value}') 14 15 title2 = '学生专业查询' 16 print(f'{title2:-^50}') 17 18 x = '' 19 while x != '#': 20 x = input('请输入学号:') 21 if x[4:8] in code_majors: 22 print(f'专业是:{code_majors.get(x[4:8])}') 23 elif x[4:8] not in code_majors: 24 print('不在这些专业中...') 25 print('查询结束')
运行截图:
task5
实验源码:
1 import random 2 lucky_day = random.randint(1, 31) 3 4 print(f'猜猜2023年5月哪一天会是你的lucky day{chr(0x1f609)}') 5 d = eval(input('你有三次机会,猜吧(1~31):')) 6 for i in range(3): 7 if d < 1 or d > 31: 8 print('地球上没有这一天啦,你是外星人吗') 9 elif d < lucky_day: 10 print('猜早啦,你的lucky day还没到呢') 11 elif d > lucky_day: 12 print('猜晚啦,你的lucky day已经过啦') 13 else: 14 print(f'哇,猜中了{chr(0x1f923)}') 15 break 16 if i == 2: 17 break 18 d = eval(input('再猜(1~31):')) 19 20 if d != lucky_day: 21 print('哇哦,次数用光啦.') 22 print(f'偷偷告诉你,5月你的lucky day是{lucky_day}号.good luck{chr(0x1f60a)}')
运行截图:
task6
实验源码:
1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], 2 '2049777002': ['音乐', '旅行'], 3 '2049777003': ['马拉松', '健身', '游戏'], 4 '2049777004': [], 5 '2049777005': ['足球', '阅读'], 6 '2049777006': ['发呆', '闲逛'], 7 '2049777007': [], 8 '2049777008': ['书法', '电影'], 9 '2049777009': ['音乐', '阅读', '电影', '漫画'], 10 '2049777010': ['数学', '推理', '音乐', '旅行'] 11 } 12 13 lst = [] 14 for v in datas.values(): 15 lst.extend(v) 16 17 s = set(lst) 18 19 d = {} 20 for i in s: 21 d.setdefault(i, lst.count(i)) 22 23 lst1 = list(d.items()) 24 lst2 = [(value, key) for key, value in lst1] 25 for value, key in sorted(lst2, reverse=True): 26 print(f'{key}:{value}')
运行截图:
task7.1
实验源码:
1 """ 2 家用电器销售系统 3 v1.3 4 """ 5 6 7 # 欢迎信息 8 print('欢迎使用家电销售系统!') 9 10 # 商品数据初始化 11 products = [['0001', '电视机', '海尔', 5999.00, 20], 12 ['0002', '冰箱', '西门子', 6998.00, 15], 13 ['0003', '洗衣机', '小天鹅', 1999.00, 10], 14 ['0004', '空调', '格力', 3900.00, 0], 15 ['0005', '热水器', '格力', 688.00, 30], 16 ['0006', '笔记本', '联想', 5699.00, 10], 17 ['0007', '微波炉', '苏泊尔', 480.00, 33], 18 ['0008', '投影仪', '松下', 1250.00, 12], 19 ['0009', '吸尘器', '飞利浦', 999.00, 9]] 20 21 # 初始化用户购物车 22 products_cart = [] 23 24 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 25 while option in ['1', '2', '3']: 26 if option == '1': 27 # 产品信息列表 28 print('产品和价格信息如下:') 29 print('**************************************************************') 30 print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % ' 价格', '%-10s' % '库存数量') 31 print('--------------------------------------------------------------') 32 for i in range(len(products)): 33 print('%-10s' % products[i][0], '%-10s' % products[i][1], '%-10s' % products[i][2], 34 '%10.2f' % products[i][3], '%10d' % products[i][4]) 35 print('--------------------------------------------------------------') 36 elif option == '2': 37 product_id = input('请输入您要购买的产品编号:') 38 while product_id not in [item[0] for item in products]: 39 product_id = input('编号不存在,请重新输入您要购买的产品编号:') 40 41 count = int(input('请输入您要购买的产品数量:')) 42 while count > products[int(product_id) - 1][4]: 43 count = int(input('数量超出库存,请重新输入您要购买的产品数量:')) 44 45 # 将所购买商品加入购物车 46 if product_id not in [item[0] for item in products_cart]: 47 products_cart.append([product_id, count]) 48 else: 49 for i in range(len(products_cart)): 50 if products_cart[i][0] == product_id: 51 products_cart[i][1] += count 52 # 更新商品列表 53 for i in range(len(products)): 54 if products[i][0] == product_id: 55 products[i][4] -= count 56 else: 57 print('购物车信息如下:') 58 print('******************************') 59 print('%-10s' % '编号', '%-10s' % '购买数量') 60 print('------------------------------') 61 for i in range(len(products_cart)): 62 print('%-10s' % products_cart[i][0], '%6d' % products_cart[i][1]) 63 print('------------------------------') 64 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 65 66 # 计算金额 67 if len(products_cart) > 0: 68 amount = 0 69 for i in range(len(products_cart)): 70 product_index = 0 71 for j in range(len(products)): 72 if products[j][0] == products_cart[i][0]: 73 product_index = j 74 break 75 price = products[product_index][3] 76 count = products_cart[i][1] 77 amount += price*count 78 79 if 5000 < amount <= 10000: 80 amount = amount*0.95 81 elif 10000 < amount <= 20000: 82 amount = amount*0.90 83 elif amount > 20000: 84 amount = amount*0.85 85 else: 86 amount = amount*1 87 print('购买成功,您需要支付%8.2f元' % amount) 88 89 # 退出系统 90 print('谢谢您的光临,下次再见!')
运行截图:
task7.2
实验源码:
1 """ 2 家用电器销售系统 3 v1.3 4 """ 5 6 7 # 欢迎信息 8 print('欢迎使用家电销售系统!') 9 10 # 商品数据初始化 11 products = [['0001', '电视机', '海尔', 5999.00, 20], 12 ['0002', '冰箱', '西门子', 6998.00, 15], 13 ['0003', '洗衣机', '小天鹅', 1999.00, 10], 14 ['0004', '空调', '格力', 3900.00, 0], 15 ['0005', '热水器', '格力', 688.00, 30], 16 ['0006', '笔记本', '联想', 5699.00, 10], 17 ['0007', '微波炉', '苏泊尔', 480.00, 33], 18 ['0008', '投影仪', '松下', 1250.00, 12], 19 ['0009', '吸尘器', '飞利浦', 999.00, 9]] 20 21 # 初始化用户购物车 22 products_cart = [] 23 24 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 25 while option in ['1', '2', '3']: 26 if option == '1': 27 # 产品信息列表 28 print('产品和价格信息如下:') 29 print('**************************************************************') 30 print(f"{'编号':<10s}{'名称':<10s}{'品牌':<10s}{'价格':<10s}{'库存数量':<10s}") 31 print('--------------------------------------------------------------') 32 for i in range(len(products)): 33 print(f'{products[i][0]:<10s}{products[i][1]:<10s}{products[i][2]:<10s}{products[i][3]:<15.2f}'f'{products[i][4]:<10d}') 34 print('--------------------------------------------------------------') 35 elif option == '2': 36 product_id = input('请输入您要购买的产品编号:') 37 while product_id not in [item[0] for item in products]: 38 product_id = input('编号不存在,请重新输入您要购买的产品编号:') 39 40 count = int(input('请输入您要购买的产品数量:')) 41 while count > products[int(product_id) - 1][4]: 42 count = int(input('数量超出库存,请重新输入您要购买的产品数量:')) 43 44 # 将所购买商品加入购物车 45 if product_id not in [item[0] for item in products_cart]: 46 products_cart.append([product_id, count]) 47 else: 48 for i in range(len(products_cart)): 49 if products_cart[i][0] == product_id: 50 products_cart[i][1] += count 51 # 更新商品列表 52 for i in range(len(products)): 53 if products[i][0] == product_id: 54 products[i][4] -= count 55 else: 56 print('购物车信息如下:') 57 print('******************************') 58 print(f"{'编号':<10s}{'购买数量':<10s}") 59 print('------------------------------') 60 for i in range(len(products_cart)): 61 print(f'{products_cart[i][0]:<15s}{products_cart[i][1]:<6d}') 62 print('------------------------------') 63 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 64 65 # 计算金额 66 if len(products_cart) > 0: 67 amount = 0 68 for i in range(len(products_cart)): 69 product_index = 0 70 for j in range(len(products)): 71 if products[j][0] == products_cart[i][0]: 72 product_index = j 73 break 74 price = products[product_index][3] 75 count = products_cart[i][1] 76 amount += price*count 77 78 if 5000 < amount <= 10000: 79 amount = amount*0.95 80 elif 10000 < amount <= 20000: 81 amount = amount*0.90 82 elif amount > 20000: 83 amount = amount*0.85 84 else: 85 amount = amount*1 86 print(f'购买成功,您需要支付{amount:<8.2f}元') 87 88 # 退出系统 89 print('谢谢您的光临,下次再见!')
运行截图:
task8.1
实验源码:
1 """ 2 家用电器销售系统 3 v1.4 4 """ 5 6 # 欢迎信息 7 print('欢迎使用家电销售系统!') 8 9 # 商品数据初始化 10 products = [ 11 {'id': '0001', 'name': '电视机', 'brand': '海尔', 'price': 5999.00, 'count': 20}, 12 {'id': '0002', 'name': '冰箱', 'brand': '西门子', 'price': 6998.00, 'count': 15}, 13 {'id': '0003', 'name': '洗衣机', 'brand': '小天鹅', 'price': 1999.00, 'count': 10}, 14 {'id': '0004', 'name': '空调', 'brand': '格力', 'price': 3900.00, 'count': 0}, 15 {'id': '0005', 'name': '热水器', 'brand': '格力', 'price': 688.00, 'count': 30}, 16 {'id': '0006', 'name': '笔记本', 'brand': '联想', 'price': 5699.00, 'count': 10}, 17 {'id': '0007', 'name': '微波炉', 'brand': '苏泊尔', 'price': 480.00, 'count': 33}, 18 {'id': '0008', 'name': '投影仪', 'brand': '松下', 'price': 1250.00, 'count': 12}, 19 {'id': '0009', 'name': '吸尘器', 'brand': '飞利浦', 'price': 999.00, 'count': 9}, 20 ] 21 22 # 初始化用户购物车 23 products_cart = [] 24 25 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 26 while option in ['1', '2', '3']: 27 if option == '1': 28 # 产品信息列表 29 print('产品和价格信息如下:') 30 print('**************************************************************') 31 print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % '价格', '%-10s' % '库存数量') 32 print('--------------------------------------------------------------') 33 for i in range(len(products)): 34 print('%-10s' % products[i]['id'], '%-10s' % products[i]['name'], '%-10s' % products[i]['brand'], 35 '%10.2f' % products[i]['price'], '%10d' % products[i]['count']) 36 print('--------------------------------------------------------------') 37 elif option == '2': 38 product_id = input('请输入您要购买的产品编号:') 39 while product_id not in [item['id'] for item in products]: 40 product_id = input('编号不存在,请重新输入您要购买的产品编号:') 41 42 count = int(input('请输入您要购买的产品数量:')) 43 while count > products[int(product_id)-1]['count']: 44 count = int(input('数量超出库存,请重新输入您要购买的产品数量:')) 45 46 # 将所购买商品加入购物车 47 if product_id not in [item['id'] for item in products_cart]: 48 products_cart.append({'id': product_id, 'count': count}) 49 else: 50 for i in range(len(products_cart)): 51 if products_cart[i].get('id') == product_id: 52 products_cart[i]['count'] += count 53 54 # 更新商品列表 55 for i in range(len(products)): 56 if products[i]['id'] == product_id: 57 products[i]['count'] -= count 58 else: 59 print('购物车信息如下:') 60 print('******************************') 61 print('%-10s' % '编号', '%-10s' % '购买数量') 62 print('------------------------------') 63 for i in range(len(products_cart)): 64 print('%-10s' % products_cart[i]['id'], '%6d' % products_cart[i]['count']) 65 print('------------------------------') 66 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 67 68 # 计算金额 69 if len(products_cart) > 0: 70 amount = 0 71 for i in range(len(products_cart)): 72 product_index = 0 73 for j in range(len(products)): 74 if products[j]['id'] == products_cart[i]['id']: 75 product_index = j 76 break 77 price = products[product_index]['price'] 78 count = products_cart[i]['count'] 79 amount += price*count 80 81 if 5000 < amount <= 10000: 82 amount = amount*0.95 83 elif 10000 < amount <= 20000: 84 amount = amount*0.90 85 elif amount > 20000: 86 amount = amount*0.85 87 else: 88 amount = amount*1 89 print('购买成功,您需要支付%8.2f元' % amount) 90 91 # 退出系统 92 print('谢谢您的光临,下次再见!')
运行截图:
task8.2
实验源码:
1 """ 2 家用电器销售系统 3 v1.4 4 """ 5 6 # 欢迎信息 7 print('欢迎使用家电销售系统!') 8 9 # 商品数据初始化 10 products = [ 11 {'id': '0001', 'name': '电视机', 'brand': '海尔', 'price': 5999.00, 'count': 20}, 12 {'id': '0002', 'name': '冰箱', 'brand': '西门子', 'price': 6998.00, 'count': 15}, 13 {'id': '0003', 'name': '洗衣机', 'brand': '小天鹅', 'price': 1999.00, 'count': 10}, 14 {'id': '0004', 'name': '空调', 'brand': '格力', 'price': 3900.00, 'count': 0}, 15 {'id': '0005', 'name': '热水器', 'brand': '格力', 'price': 688.00, 'count': 30}, 16 {'id': '0006', 'name': '笔记本', 'brand': '联想', 'price': 5699.00, 'count': 10}, 17 {'id': '0007', 'name': '微波炉', 'brand': '苏泊尔', 'price': 480.00, 'count': 33}, 18 {'id': '0008', 'name': '投影仪', 'brand': '松下', 'price': 1250.00, 'count': 12}, 19 {'id': '0009', 'name': '吸尘器', 'brand': '飞利浦', 'price': 999.00, 'count': 9}, 20 ] 21 22 # 初始化用户购物车 23 products_cart = [] 24 25 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 26 while option in ['1', '2', '3']: 27 if option == '1': 28 # 产品信息列表 29 print('产品和价格信息如下:') 30 print('**************************************************************') 31 print(f"{'编号':<10s}{'名称':<10s}{'品牌':<10s}{'价格':<10s}{'库存数量':<10s}") 32 print('--------------------------------------------------------------') 33 for i in range(len(products)): 34 print(f"{products[i]['id']:<10s}{products[i]['name']:<10s}{products[i]['brand']:<10s}" 35 f"{products[i]['price']:<15.2f}{products[i]['count']:<10d}") 36 print('--------------------------------------------------------------') 37 elif option == '2': 38 product_id = input('请输入您要购买的产品编号:') 39 while product_id not in [item['id'] for item in products]: 40 product_id = input('编号不存在,请重新输入您要购买的产品编号:') 41 42 count = int(input('请输入您要购买的产品数量:')) 43 while count > products[int(product_id)-1]['count']: 44 count = int(input('数量超出库存,请重新输入您要购买的产品数量:')) 45 46 # 将所购买商品加入购物车 47 if product_id not in [item['id'] for item in products_cart]: 48 products_cart.append({'id': product_id, 'count': count}) 49 else: 50 for i in range(len(products_cart)): 51 if products_cart[i].get('id') == product_id: 52 products_cart[i]['count'] += count 53 54 # 更新商品列表 55 for i in range(len(products)): 56 if products[i]['id'] == product_id: 57 products[i]['count'] -= count 58 else: 59 print('购物车信息如下:') 60 print('******************************') 61 print(f"{'编号':<15s}{'购买数量':<6s}") 62 print('------------------------------') 63 for i in range(len(products_cart)): 64 print(f"{products_cart[i]['id']:<10s}{products_cart[i]['count']:<6d}") 65 print('------------------------------') 66 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 67 68 # 计算金额 69 if len(products_cart) > 0: 70 amount = 0 71 for i in range(len(products_cart)): 72 product_index = 0 73 for j in range(len(products)): 74 if products[j]['id'] == products_cart[i]['id']: 75 product_index = j 76 break 77 price = products[product_index]['price'] 78 count = products_cart[i]['count'] 79 amount += price*count 80 81 if 5000 < amount <= 10000: 82 amount = amount*0.95 83 elif 10000 < amount <= 20000: 84 amount = amount*0.90 85 elif amount > 20000: 86 amount = amount*0.85 87 else: 88 amount = amount*1 89 print(f'购买成功,您需要支付{amount:<8.2f}元') 90 91 # 退出系统 92 print('谢谢您的光临,下次再见!')
运行截图:
标签:语句,count,product,编程,数据类型,cart,products,print,id From: https://www.cnblogs.com/xrl1113/p/17352317.html