实验任务1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import random 2 print('用列表存储随机整数: ') 3 lst = [random.randint(0, 100) for i in range(5)] 4 print(lst) 5 print('\n用集合存储随机整数: ') 6 s1 = {random.randint(0, 100) for i in range(5)} 7 print(s1) 8 print('\n用集合存储随机整数: ') 9 s2 = set() 10 while len(s2) < 5: 11 s2.add(random.randint(0, 100)) 12 print(s2)View Code
问题回答:1. 0到100,能取到100;
2. 0到4,不包括 ;1到4,不包括。
3. 不一定,可能长度是4。
4. 一定。
实验任务2
2.1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 lst = [55, 92, 88, 79, 96] 2 i = 0 3 while i < len(lst): 4 print(lst[i], end=' ') 5 i += 1 6 print() 7 8 for i in range(len(lst)): 9 print(lst[i], end=' ') 10 print() 11 12 for i in lst: 13 print(i, end=' ') 14 print()View Code
2.2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 book_info = {'isbn': '978-7-5356-8297-0', 2 '书名': '白鲸记', 3 '作者': '克里斯多夫.夏布特', 4 '译者': '高文婧', 5 '出版社': '湖南美术出版社', 6 '售价': 82 7 } 8 for key, value in book_info.items(): 9 print(f'{key}: {value}') 10 print() 11 12 for item in book_info.items(): 13 print(f'{item[0]}: {item[1]}') 14 print() 15 16 for value in book_info.values(): 17 print(value, end = ' ') 18 print() 19 20 for key in book_info.keys(): 21 print(book_info[key], end = ' ')View Code
2.3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, 2 {'书名': '局外人', '作者': '阿尔贝.加缪'}, 3 {'书名': '设计中的设计', '作者': '原研哉'}, 4 {'书名': '万历十五年', '作者': '黄仁宇'}, 5 {'书名': '刀锋', '作者': '毛姆'} 6 ] 7 i = 0 8 while i < 5: 9 s = book_infos[i] 10 v1 = s['书名'] 11 v2 = s['作者'] 12 print(f'{i+1}. 《{v1}》, {v2}') 13 i += 1View Code
实验任务3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 test = '''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 test1 = test.lower() 24 import string 25 t = list(string.ascii_lowercase) 26 s = {} 27 i = 0 28 while i < 26: 29 ans = test1.count(t[i]) 30 s.update({t[i]:ans}) 31 i += 1 32 f = zip(s.values(),s.keys()) 33 c = sorted(f,reverse=True) 34 a_sorted = dict(c) 35 for item in a_sorted.items(): 36 print(f'{item[1]}:{item[0]}')View Code
实验任务4
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 code_majors = {'8326' : '地信类', 2 '8329' : '计算机类', 3 '8330' : '气科类', 4 '8336' : '防灾工程', 5 '8345' : '海洋科学', 6 '8382' : '气象工程' 7 } 8 9 print(f'{"专业代号信息":-^50}') 10 for key,value in code_majors.items(): 11 print(f'{key}: {value}') 12 print(f'{"学生专业查询":-^50}') 13 number = input('请输入学号:') 14 while number != '#': 15 if number[4:8] in code_majors: 16 print('专业是:' + code_majors[number[4:8]]) 17 number = input('请输入学号:') 18 19 else: 20 print('不在这些专业中...') 21 number = input('请输入学号:') 22 else: 23 print('查询结束...')View Code
实验任务5
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import random 2 lst = [random.randint(1, 31) for i in range(1)] 3 mdays = [i for i in range(1,32)] 4 print('猜猜2023年5月那一天会是你的lucky day' + chr(0x1f609)) 5 day = input('你有三次机会,猜吧(1~31):') 6 i = 1 7 while i <= 2: 8 if int(day) in mdays: 9 if int(day) < lst[0]: 10 print('猜早啦,你的lucky day还没到呢') 11 day = input('再猜(1~31):') 12 i += 1 13 elif int(day) > lst[0]: 14 print('猜晚啦,你的lucky day已经过啦') 15 day = input('再猜(1~31):') 16 i += 1 17 else: 18 print(f'哇,猜中了{chr(0x1f923)}') 19 break 20 21 else: 22 print('地球上没有这一天啦,你是外星人吗') 23 day = input('再猜(1~31):') 24 i += 1 25 else: 26 print('哇哦,次数用光啦') 27 print(f'偷偷告诉你,5月你的lucky day是{lst[0]}号.good luck{chr(0x1f60a)}')View Code
实验任务6
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], 2 '2049777002': ['音乐', '旅行'], 3 '2049777003': ['马拉松', '健身', '游戏'], 4 '2049777004': [], 5 '2049777005': ['足球', '阅读'], 6 '2049777006': ['发呆', '闲逛'], 7 '2049777007': [], 8 '2049777008': ['书法', '电影'], 9 '2049777009': ['音乐', '阅读', '电影', '漫画'], 10 '2049777010': ['数学', '推理', '音乐', '旅行'] 11 } 12 lst1 = list(datas.items()) 13 lst2 = [] 14 i = 0 15 while i < len(lst1): 16 lst2.append(lst1[i][1]) 17 i += 1 18 lst3 = [] 19 for a in range(0,len(lst2)): 20 for b in range(0,len(lst2[a])): 21 lst3.append(lst2[a][b]) 22 lst4 = list(set(lst3)) 23 d = {} 24 for i in range(len(lst4)): 25 t = {lst4[i]:lst3.count(lst4[i])} 26 d.update(t) 27 d1 = sorted(d.items(), key = lambda x:x[1], reverse = True) 28 for i in d1: 29 print(f'{i[0]}:{i[1]}')View Code
实验任务7
7-1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 """ 2 家用电器销售系统 3 v1.3 4 """ 5 6 #欢迎信息 7 print('欢迎使用家用电器销售系统!') 8 9 #商品数据初始化 10 products=[ 11 ['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 #初始化用户购物车 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('*' * 60) 31 print(f"{'编号': <10} {'名称': <10} {'品牌': <10} {'价格': <10} {'库存数量': <10} ") 32 print('-' * 65) 33 for i in range(len(products)): 34 print(f'{products[i][0]: <10} {products[i][1]: <10} {products[i][2]: <10} {products[i][3]: <10} {products[i][4]: <10}') 35 print('-' * 65) 36 elif option == '2': 37 products_id = input('请输入您要购买的产品编号:') 38 while products_id not in [item[0] for item in products]: 39 products_id = input('编号不存在,请重新输入您要购买的产品编号:') 40 41 count = int(input('请输入您要购买的产品数量:')) 42 while count > products[int(products_id) - 1][4]: 43 count = int(input('数据超出库存,请重新输入您要购买的产品数量:')) 44 45 #将所购买商品加入购物车 46 if products_id not in [item[0] for item in products_cart]: 47 products_cart.append([products_id,count]) 48 else: 49 for i in range(len(products_cart)): 50 if products_cart[i][0] == products_id: 51 products_cart[i][1] += count 52 53 #更新商品列表 54 for i in range(len(products)): 55 if products[i][0] == products_id: 56 products[i][4] -= count 57 58 else: 59 print('购物车信息如下:') 60 print('*' * 60) 61 print(f"{'编号': <10} {'购买数量': <10}") 62 print('-' * 65) 63 for i in range(len(products_cart)): 64 print(f"{products_cart[i][0]: <10} {products_cart[i][1]: > 6}") 65 print('-' * 65) 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][0] == products_cart[i][0]: 75 product_index = j 76 break 77 price = products[product_index][3] 78 count = products_cart[i][1] 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:.2f}元') 90 91 #退出系统 92 print('谢谢您的光临,下次再见!')View Code
7-2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 """ 2 家用电器销售系统 3 v1.3 4 """ 5 #欢迎信息 6 print('欢迎使用家用电器销售系统!') 7 #商品数据初始化 8 products=[ 9 ['0001','电视机','海尔',5999.00,20], 10 ['0002','冰箱','西门子',6998.00,15], 11 ['0003','洗衣机','小天鹅',1999.00,10], 12 ['0004','空调','格力',3900.00,0], 13 ['0005','热水器','格力',688.00,30], 14 ['0006','笔记本','联想',5699.00,10], 15 ['0007','微波炉','苏泊尔',480.00,33], 16 ['0008','投影仪','松下',1250.00,12], 17 ['0009','吸尘器','飞利浦',999.00,9], 18 ] 19 #初始化用户购物车 20 products_cart=[] 21 option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 22 while option in ['1','2','3']: 23 if option=='1': 24 #产品信息列表 25 print('产品和价格信息如下:') 26 print('**********************************************************') 27 print('{:<10} {:<10} {:<10} {:<10} {:<10}'.format('编号','名称','品牌','价格','库存数量')) 28 print('----------------------------------------------------------') 29 for i in range(len(products)): 30 print('{:<10}'.format(products[i][0]),'{:<10}'.format(products[i][1]),'{:<10}'.format(products[i][2]),'{:<10.2f}'.format(products[i][3]),'{:>10}'.format(products[i][4])) 31 print('----------------------------------------------------------') 32 elif option=='2': 33 product_id= input('请输入您要购买的产品编号:') 34 while product_id not in[item[0] for item in products]: 35 product_id=input('编号不存在,请重新输入您要购买的产品编号:') 36 37 count=int(input('请输入您要购买的产品数量:')) 38 while count> products[int( product_id)-1][4]: 39 count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) 40 41 #将所购买商品加入购物车 42 if product_id not in [item[0] for item in products_cart]: 43 products_cart.append([product_id,count]) 44 else: 45 for i in range(len(products_cart)): 46 if products_cart[i][0]==product_id: 47 products_cart[i][1]+=count 48 49 #更新商品列表 50 for i in range(len(products)): 51 if products[i][0]==product_id: 52 products[i][4]-=count 53 else: 54 print('购物车信息如下:') 55 print('***********************************************************') 56 print('{:<10} {:<10} '.format('编号','购买数量')) 57 print('----------------------------------------------------------') 58 for i in range(len(products_cart)): 59 print('{:10s}'.format(products_cart[i][0]),'{:6d}'.format(products_cart[i][1])) 60 print('----------------------------------------------------------') 61 option=input('操作成功!请选择您的操作:1—查看物品;2-购物;3-查看购物车;其它-结账') 62 #计算金额 63 if len( products_cart)>0: 64 amount=0 65 for i in range( len( products_cart)): 66 product_index=0 67 for j in range(len(products)): 68 if products[j][0]== products_cart[i][0]: 69 product_index=j 70 break 71 price= products[ product_index][3] 72 count= products_cart[i][1] 73 amount+=price*count 74 if 5000<amount<=10000: 75 amount=amount*0.95 76 elif 10000<amount <=20000: 77 amount=amount*0.90 78 elif amount>20000: 79 amount=amount*0.85 80 else: 81 amount=amount*1 82 print('购买成功,您需要支付%8.2f元'%amount) 83 #退出系统 84 print('谢谢您的光临,下次再见!') 85 print('谢谢您的光临,下次再见!')View Code
实验任务8
8-1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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('*' * 60) 31 print('%-10s' %'编号', '%-10s' %'名称', '%-10s' %'品牌', '%-10s' %'价格', '%-10s' %'库存数量') 32 print('-' * 65) 33 for i in range(len(products)): 34 print('%-10s' %products[i]['id'], '%-10s' %products[i]['name'], '%-10s' %products[i]['brand'], 35 '%-10s' %products[i]['price'], '%-10s' %products[i]['count']) 36 print('-' * 65) 37 elif option == '2': 38 products_id = input('请输入您要购买的产品编号:') 39 while products_id not in [item['id'] for item in products]: 40 products_id = input('编号不存在,请重新输入您要购买的产品编号:') 41 42 count = int(input('请输入您要购买的产品数量:')) 43 while count > products[int(products_id) - 1]['count']: 44 count = int(input('数据超出库存,请重新输入您要购买的产品数量:')) 45 46 #将所购买商品加入购物车 47 if products_id not in [item['id'] for item in products_cart]: 48 products_cart.append({'id':products_id, 'count':count}) 49 else: 50 for i in range(len(products_cart)): 51 if products_cart[i].get('id') == products_id: 52 products_cart[i]['count'] += count 53 54 #更新商品列表 55 for i in range(len(products)): 56 if products[i]['id'] == products_id: 57 products[i]['count'] -= count 58 59 else: 60 print('购物车信息如下:') 61 print('*' * 60) 62 print('%-10s' %'编号', '%-10s' %'购买数量') 63 print('-' * 65) 64 for i in range(len(products_cart)): 65 print('%-10s' %products_cart[i]['id'], '%6d' %products_cart[i]['count']) 66 print('-' * 65) 67 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 68 69 #计算金额 70 if len(products_cart) > 0: 71 amount = 0 72 for i in range(len(products_cart)): 73 product_index = 0 74 for j in range(len(products)): 75 if products[j]['id'] == products_cart[i]['id']: 76 product_index = j 77 break 78 price = products[product_index]['price'] 79 count = products_cart[i]['count'] 80 amount += price * count 81 82 if 5000 < amount <= 10000: 83 amount = amount * 0.95 84 elif 10000 < amount <= 20000: 85 amount = amount * 0.90 86 elif amount > 20000: 87 amount = amount * 0.85 88 else: 89 amount = amount * 1 90 print('购买成功,您需要支付%8.2f 元' %amount) 91 92 #退出系统 93 print('谢谢您的光临,下次再见!')View Code
8-2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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('*' * 60) 31 print(f"{'编号': <10} {'名称': <10} {'品牌': <10} {'价格': <10} {'库存数量': <10} ") 32 print('-' * 65) 33 for i in range(len(products)): 34 print(f"{products[i]['id']: <10} {products[i]['name']: <10} {products[i]['brand']: <10} {products[i]['price']: <10} {products[i]['count']: <10}") 35 print('-' * 65) 36 elif option == '2': 37 products_id = input('请输入您要购买的产品编号:') 38 while products_id not in [item['id'] for item in products]: 39 products_id = input('编号不存在,请重新输入您要购买的产品编号:') 40 41 count = int(input('请输入您要购买的产品数量:')) 42 while count > products[int(products_id) - 1]['count']: 43 count = int(input('数据超出库存,请重新输入您要购买的产品数量:')) 44 45 #将所购买商品加入购物车 46 if products_id not in [item['id'] for item in products_cart]: 47 products_cart.append({'id':products_id, 'count':count}) 48 else: 49 for i in range(len(products_cart)): 50 if products_cart[i].get('id') == products_id: 51 products_cart[i]['count'] += count 52 53 #更新商品列表 54 for i in range(len(products)): 55 if products[i]['id'] == products_id: 56 products[i]['count'] -= count 57 58 else: 59 print('购物车信息如下:') 60 print('*' * 60) 61 print(f"{'编号': <10} {'购买数量': <10}") 62 print('-' * 65) 63 for i in range(len(products_cart)): 64 print(f"{products_cart[i]['id']: <10} {products_cart[i]['count']: > 6}") 65 print('-' * 65) 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:.2f}元') 90 91 #退出系统 92 print('谢谢您的光临,下次再见!')View Code
标签:语句,count,编程,数据类型,cart,amount,products,print,id From: https://www.cnblogs.com/202280060019x/p/17332827.html