#1
实验内容:
1 #1 2 import random 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)View Code
实验结果:
问题1:【0,100】,能
2:【0,4】,不包括
【1,4】,不包括
3:不一定,集合里不会有重复的元素
4:是
#2.1
实验内容:
1 #2 2 #列表遍历 3 lst=[55,92,88,79,96] 4 #遍历方式1:使用while+索引 5 i=0 6 while i<len(lst): 7 print(lst[i],end=' ') 8 i+=1 9 print() 10 11 #遍历方式2:使用for+索引 12 for i in range(len(lst)): 13 print(lst[i],end=' ') 14 print() 15 16 #遍历方式3:使用for...in 17 for i in lst: 18 print(i,end=' ') 19 print()View Code
实验结果:
#2.2
实验内容:
1 #2.2 2 #字典遍历 3 book_info={'isbn':'978-7-5356-8297-0', 4 '书名':'白鲸记', 5 '作者':'克里斯多夫.夏布特', 6 '译者':'高文婧', 7 '出版社':'湖南美术出版社', 8 '售价':82 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=' ')View Code
实验结果:
#2.3
实验内容:
1 #2.3 2 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, 3 {'书名': '局外人', '作者': '阿尔贝.加缪'}, 4 {'书名': '设计中的设计', '作者': '原研哉'}, 5 {'书名': '万历十五年', '作者': '黄仁宇'}, 6 {'书名': '刀锋', '作者': '毛姆'} 7 ] 8 j=1 9 for i in book_infos: 10 print(f'{j}.'+'《'+i['书名']+'》'+','+i['作者']) 11 j+=1View Code
实验结果:
#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 text=text.lower() 23 l=[] 24 p=[] 25 for i in range(97,123): 26 i=chr(i) 27 l.append((text.count(i),i)) 28 l=sorted(l,reverse=True) 29 for j in range(len(l)): 30 print(f'{l[j][1]}:{l[j][0]}')View Code
实验结果:
#4
实验内容:
1 #4 2 print('{0:-^50}'.format('专业代号信息')) 3 x={'8326':'地信类','8329':'计算机类','8330':'气科类','8336':'防灾工程','8345':'海洋科学','8382':'气象工程'} 4 for key,value in x.items(): 5 print(f'{key}: {value}') 6 print('{0:-^50}'.format('学生专业查询')) 7 y=input('请输入学号:') 8 while y!='#': 9 y=y[4:8] 10 if y in x.keys(): 11 print(f'专业是:{x[y]}') 12 else: 13 print('不在这些专业中...') 14 y=input('请输入学号:') 15 print('查询结束...')View Code
实验结果:
#5
实验内容:
1 #5 2 import random 3 x=random.randint(1,31) 4 print(f'猜猜2023年5月哪一天会是你的lucky day{chr(0x1f609)}') 5 y=int(input('你有三次机会,猜吧(1~31):')) 6 c=0 7 while c<2 and y!=x: 8 if y in list(range(1,32)): 9 if y<x: 10 print('猜早啦,你的lucky day还没到呢') 11 elif y>x: 12 print('猜晚啦,你的lucky day过啦') 13 else: 14 print('地球上没有这一天啦,你是外星人吗') 15 c+=1 16 y=int(input('再猜(1~31):')) 17 if c==3: 18 if y!=x: 19 print('哇哦,次数用光啦.') 20 print(f'偷偷告诉你,5月你的lucky day是{x}号.good luck{chr(0x1f60a)}') 21 else : 22 print(f'哇,猜中了{chr(0x1f923)}') 23 else: 24 print(f'哇,猜中了{chr(0x1f923)}')View Code
实验结果:
#6
实验内容:
1 datas={'2049777001':['篮球','羽毛球','美食','漫画'], 2 '2049777002':['音乐','旅行'], 3 '2049777003':['马拉松','健身','游戏'], 4 '2049777004':[], 5 '2049777005':['足球','阅读'], 6 '2049777006':['发呆','闲逛'], 7 '2049777007':[], 8 '2049777008':['书法','电影'], 9 '2049777009':['音乐','阅读','电影','漫画'], 10 '2049777010':['数学','推理','音乐','旅行'] 11 } 12 x={} 13 y=[] 14 for i in datas.values(): 15 for j in i: 16 if j not in x: 17 x[j]=1 18 else: 19 x[j]+=1 20 for x1,x2 in x.items(): 21 y.append((x2,x1)) 22 y=sorted(y,reverse=True) 23 for o in range(0,len(y)): 24 print(f'{y[o][1]}:{y[o][0]}')View Code
实验结果:
#7.1
实验内容:
1 #7.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('*'*40) 27 print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量') 28 print('-'*30) 29 for i in range(len(products)): 30 print('%-10s'%products[i][0],'%-10s'%products[i][1],'%-10s'%products[i][2],'%10.2f'%products[i][3 31 ],'%10d'%products[i][4]) 32 print('-'*30) 33 elif option=='2': 34 product_id=input('请输入您要购买的产品编号:') 35 while product_id not in [item[0] for item in products]: 36 product_id=input('编号不存在,请重新输入您要购买的产品编号:') 37 count=int(input('请输入您要购买的产品数量:')) 38 while count>products[int(product_id)-1][4]: 39 count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) 40 #将所购买商品加入购物车 41 if product_id not in [item[0] for item in products_cart]: 42 products_cart.append([product_id,count]) 43 else: 44 for i in range(len(products_cart)): 45 if products_cart[i][0]==product_id: 46 products_cart[i][1]+=count 47 #更新商品列表 48 for i in range(len(products)): 49 if products[i][0]==product_id: 50 products[i][4]-=count 51 else: 52 print('购物车信息如下:') 53 print('*'*40) 54 print('%-10s'%'编号','%-10s'%'购买数量') 55 print('-'*30) 56 for i in range(len(products_cart)): 57 print('%-10s'%products_cart[i][0],'%6d'%products_cart[i][1]) 58 print('-'*30) 59 option=input('操作成功!请选择你的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 60 61 #计算金额 62 if len(products_cart)>0: 63 amount=0 64 for i in range(len(products_cart)): 65 product_index=0 66 for j in range(len(products)): 67 if products[j][0]==products_cart[i][0]: 68 product_index=j 69 break 70 price=products[product_index][3] 71 count=products_cart[i][1] 72 amount+=price*count 73 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('谢谢您的光临,下次再见!')View Code
实验结果:
#7.2
实验内容:
1 #7.2 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('*'*40) 27 print('{0:<10}{1:<10}{2:<10}{3:<10}{4:<10}'.format('编号','名称','品牌','价格','库存数量')) 28 print('-'*30) 29 for i in range(len(products)): 30 print('{0:<10}{1:<10}{2:<10}{3:<10}{4:<10}'.format(products[i][0],products[i][1],products[i][2],products[i][3],products[i][4])) 31 print('-'*30) 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 count=int(input('请输入您要购买的产品数量:')) 37 while count>products[int(product_id)-1][4]: 38 count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) 39 #将所购买商品加入购物车 40 if product_id not in [item[0] for item in products_cart]: 41 products_cart.append([product_id,count]) 42 else: 43 for i in range(len(products_cart)): 44 if products_cart[i][0]==product_id: 45 products_cart[i][1]+=count 46 #更新商品列表 47 for i in range(len(products)): 48 if products[i][0]==product_id: 49 products[i][4]-=count 50 else: 51 print('购物车信息如下:') 52 print('*'*40) 53 print('{0:<10}{1:<10}'.format('编号','购买数量') 54 print('-'*30) 55 for i in range(len(products_cart)): 56 print('{0:<10}{1:<10}'.format(products_cart[i][0],products_cart[i][1])) 57 print('-'*30) 58 option=input('操作成功!请选择你的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 59 60 #计算金额 61 if len(products_cart)>0: 62 amount=0 63 for i in range(len(products_cart)): 64 product_index=0 65 for j in range(len(products)): 66 if products[j][0]==products_cart[i][0]: 67 product_index=j 68 break 69 price=products[product_index][3] 70 count=products_cart[i][1] 71 amount+=price*count 72 73 if 5000<amount<=10000: 74 amount=amount*0.95 75 elif 10000<amount<=20000: 76 amount=amount*0.90 77 elif amount>20000: 78 amount=amount*0.85 79 else: 80 amount=amount*1 81 print('购买成功,您需要支付{:8.2f}元'.format(amount)) 82 #退出系统 83 print('谢谢您的光临,下次再见!')View Code
实验结果:
#8.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('*'*50) 31 print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量') 32 print('-'*50) 33 for i in range(len(products)): 34 print('%-10s'%products[i]['id'],'%-10s'%products[i]['name'],'%-10s'%products[i]['brand'],'%10.2f'%products[i]['price'],'%10d'%products[i]['count']) 35 print('-'*50) 36 elif option=='2': 37 product_id=input('请输入您要购买的产品编号:') 38 while product_id not in [item['id'] for item in products]: 39 product_id=input('编号不存在,请重新输入您要购买的产品编号:') 40 count=int(input('请输入您要购买的产品数量:')) 41 while count>products[int(product_id)-1]['count']: 42 count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) 43 44 #将所购买商品加入购物车 45 if product_id not in [item['id'] for item in products_cart]: 46 products_cart.append({'id':product_id,'count':count}) 47 else: 48 for i in range(len(products_cart)): 49 if products_cart[i].get('id')==product_id: 50 products_cart[i]['count']+=count 51 #更新商品列表 52 for i in range(len(products)): 53 if products[i]['id']==product_id: 54 products[i]['count']-=count 55 else: 56 print('购物车信息如下:') 57 print('*'*40) 58 print('%-10s'%'编号','%-10s'%'购买数量') 59 print('-'*30) 60 for i in range(len(products_cart)): 61 print('%-10s'%products_cart[i]['id'],'%6d'%products_cart[i]['count']) 62 print('-'*30) 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]['id']==products_cart[i]['id']: 72 product_index=j 73 break 74 price=products[product_index]['price'] 75 count=products_cart[i]['count'] 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('购买成功,您需要支付%8.2f元'%amount) 87 #退出系统 88 print('谢谢您的光临,下次再见!')View Code
实验结果:
#8.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('*'*50) 31 print('{0:<10}{1:<10}{2:<10}{3:<10}{4:<10}'.format('编号','名称','品牌','价格','库存数量')) 32 print('-'*50) 33 for i in range(len(products)): 34 print('{0:<10}{1:<10}{2:<10}{3:<10}{4:<10}'.format(products[i]['id'],products[i]['name'],products[i]['brand'],products[i]['price'],products[i]['count'])) 35 print('-'*50) 36 elif option=='2': 37 product_id=input('请输入您要购买的产品编号:') 38 while product_id not in [item['id'] for item in products]: 39 product_id=input('编号不存在,请重新输入您要购买的产品编号:') 40 count=int(input('请输入您要购买的产品数量:')) 41 while count>products[int(product_id)-1]['count']: 42 count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) 43 44 #将所购买商品加入购物车 45 if product_id not in [item['id'] for item in products_cart]: 46 products_cart.append({'id':product_id,'count':count}) 47 else: 48 for i in range(len(products_cart)): 49 if products_cart[i].get('id')==product_id: 50 products_cart[i]['count']+=count 51 #更新商品列表 52 for i in range(len(products)): 53 if products[i]['id']==product_id: 54 products[i]['count']-=count 55 else: 56 print('购物车信息如下:') 57 print('*'*40) 58 print('{0:<10}{1:<10}'.format('编号','购买数量')) 59 print('-'*30) 60 for i in range(len(products_cart)): 61 print('{0:<10}{1:<10}'.format(products_cart[i]['id'],products_cart[i]['count'])) 62 print('-'*30) 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]['id']==products_cart[i]['id']: 72 product_index=j 73 break 74 price=products[product_index]['price'] 75 count=products_cart[i]['count'] 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('购买成功,您需要支付{:8.2f}元'.format(amount)) 87 #退出系统 88 print('谢谢您的光临,下次再见!')View Code
实验结果:
标签:count,product,cart,products,print,实验,id From: https://www.cnblogs.com/047lishiqi/p/17332767.html