实验三 控制语句与组合数据类型应用编程
实验三 控制语句与组合数据类型应用编程
实验任务一
程序源代码:
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)
实验结果:

答案:问题一:【1,100】,能
问题二:[0,1,2,3,4],不包括
问题三:不一定,随机生成的五个数有可能有重复
问题四:一定,while len(s2)<5 这一循环条件表明len(s2)=5
实验任务二
task2-1:
程序源代码:
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()
实验结果:

task2-2:
程序源代码:
实验结果:
实验任务三
程序源代码:
1 x='''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 a=x.lower() 23 b={} 24 z=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] 25 for i in z: 26 shu=a.count(i) 27 b[i]=shu 28 fan=[(x[1],x[0]) for x in list(b.items())] 29 fan.sort(reverse=True) 30 for i in range(0,26): 31 print(f'{fan[i][0]}:{fan[i][1]}')
实验结果:

实验任务四
程序源代码:
1 print(8345,':',dai[8345]) 2 print(8382,':',dai[8382]) 3 print('{0:-^50}'.format('学生专业查询')) 4 x=input('请输入专业号:') 5 z=list(x) 6 while x!='#': 7 if int(z[4]+z[5]+z[6]+z[7]) not in dai.keys(): 8 print('不在这些专业中。。。') 9 break 10 else: 11 print('专业是:',dai[int(z[4]+z[5]+z[6]+z[7])]) 12 x=input('请输入专业号:') 13 z=list(x) 14 else: 15 print('查询结束。。。')
实验结果:

实验五
程序源代码:
1 import random 2 print('猜猜2023年5月哪一天会是你的lucky day',chr(0x1f609)) 3 x=random.randint(1,31) 4 y=int(input('你有三次机会,猜吧(1~31):')) 5 z=0 6 while x!=y: 7 if y<x: 8 print('猜早了,你的lucky day还没到呢') 9 elif y>x: 10 print('猜晚了,你的lucky day已经过了') 11 elif y==0: 12 print('地球上没有这一天啦,你是外星人吗') 13 z+=1 14 if z==3: 15 print('哇哦,次数用光了。') 16 print(f'偷偷告诉你,5月你的luckyday是{x}号.good luck') 17 break 18 elif z<3: 19 y=int(input('再猜(1~31):')) 20 elif z>3: 21 break 22 else: 23 print(f'哇,猜中啦{chr(0x1f923)}')
实验结果:


实验六
程序源代码:
1 for i in range(len(x)): 2 y+=x[i] 3 z=list(set(y)) 4 k={} 5 for i in range(len(z)): 6 k[z[i]]=y.count(z[i]) 7 kfa=list(k) 8 kfa1=list(k.values()) 9 kfa2=[(kfa[i],kfa1[i]) for i in range(len(kfa))] 10 kfan=[(kfa2[i][1],kfa2[i][0]) for i in range(len(kfa2))] 11 a=sorted(kfan) 12 for i in range(len(a)): 13 print(f'{a[i][0]}:{a[i][1]}')
实验结果

实验七
task7-1:
程序源代码:
print('欢迎使用家用电器销售系统!') pro=[['0001','电视机','海尔',5999.00,20], ['0002','冰箱','西门子',6998.00,15], ['0003','洗衣机','小天鹅',1999.00,10], ['0004','空调','格力',3900.00,0], ['0005','热水器','格力',688.00,30], ['0006','笔记本','联想',5699.00,10], ['0007','微波炉','苏泊尔',480.00,33], ['0008','投影仪','松下',1250.00,12], ['0009','吸尘器','飞利浦',999.00,9]] products_cart=[] option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') while option in['1','2','3',]: if option=='1': print('产品和价格信息如下') print('***********************************************************') print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % '价格', '%-10s' % '库存数量') print('-----------------------------------------------------------') for i in range(len(pro)): print('%-10s'%pro[i][0],'%-10s'%pro[i][1],'%-10s'%pro[i][2],'%10.2f'%pro[i][3],'%10d'%pro[i][4]) print('-----------------------------------------------------------') elif option=='2': pro_id=input('请输入您要购买的产品编号:') while pro_id not in [item[0]for item in pro]: pro_id=input('编号不存在,请重新输入您要购买的产品编号:') count=int(input('请输入您要购买的产品数量:')) while count > pro[int(pro_id)-1][4]: count=input('数量超出库存。请重新输入您要购买的产品数量:') if pro_id not in [item[0] for item in products_cart]: products_cart.append([pro_id,count]) else: for i in range(len(products_cart)): if products_cart[i][0]==pro_id: products_cart[i][1]+=count else: print('购物车信息如下:') print('************************************') print('%-10s'%'编号','%-10s'%'购买数量') print('------------------------------------') for i in range(len(products_cart)): print('%-10s'%products_cart[i][0],'%6d'%products_cart[i][1]) print('------------------------------------') option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。') if len(products_cart)>0: amount=0 for i in range(len(products_cart)): pro_index=0 for j in range(len(pro)): if pro[j][0]==products_cart[i][0]: pro_index=j break price=pro[pro_index][3] count=products_cart[i][1] amount+=price*count if 5000 < amount <= 10000: amount=amount*0.95 elif 10000 < amount <= 20000: amount = amount * 0.9 elif 20000 < amount : amount = amount * 0.85 else: amount=amount*1 print('购买成功,您需要支付%8.2f元'%amount) 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('谢谢您的光临,下次再见!')
实验结果:

实验八
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'],'%10.2f'%pro[i]['price'],'%10d'%pro[i]['count']) 22 print('-----------------------------------------------------------') 23 elif option == '2': 24 pro_id = input('请输入您要购买的产品编号:') 25 while pro_id not in [item['id'] for item in pro]: 26 pro_id = input('编号不存在,请重新输入您要购买的产品编号:') 27 count=int(input('请输入您要购买的产品数量:')) 28 while count > pro[int(pro_id)-1]['count']: 29 count=input('数量超出库存。请重新输入您要购买的产品数量:') 30 if pro_id not in [item['id'] for item in products_cart]: 31 products_cart.append({'id':pro_id,'count':count}) 32 else: 33 for i in range(len(products_cart)): 34 if products_cart[i].get('id')==pro_id: 35 products_cart[i]['count']+=count 36 37 for i in range(len(pro)) : 38 if pro[i]['id']==pro_id: 39 pro[i]['count']-=count 40 else: 41 print('购物车信息如下:') 42 print('************************************') 43 print('%-10s'%'编号','%-10s'%'购买数量') 44 print('------------------------------------') 45 for i in range(len(products_cart)): 46 print('%-10s'%products_cart[i]['id'],'%6d'%products_cart[i]['count']) 47 print('------------------------------------') 48 option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。') 49 if len(products_cart)>0: 50 amount=0 51 for i in range(len(products_cart)): 52 pro_index=0 53 for j in range(len(pro)): 54 if pro[j]['id']==products_cart[i]['id']: 55 pro_index=j 56 break 57 price=pro[pro_index]['price'] 58 count=products_cart[i]['count'] 59 amount+=price*count 60 if 5000 < amount <= 10000: 61 amount = amount * 0.95 62 elif 10000 < amount <= 20000: 63 amount = amount * 0.9 64 elif 20000 < amount: 65 amount = amount * 0.85 66 else: 67 amount = amount * 1 68 print('购买成功,您需要支付%8.2f元' % amount) 69 print('谢谢您的光临,下次再见!')
实验结果:

task8-2
程序源代码:
1 """ 2 家用电器销售系统 3 v1.4 4 """ 5 #欢迎信息 6 print('欢迎使用家用电器销售系统!') 7 #商品数据初始化 8 products = [ 9 {'id':"0001", 'name':'电视机', 'brand':'海尔', 'price':5999.00, 'count':20}, 10 {'id':"0002", 'name':'冰箱', 'brand':'西门子', 'price':6998.00, 'count':15}, 11 {'id':"0003", 'name':'洗衣机', 'brand':'小天鹅', 'price':1999.00, 'count':10}, 12 {'id':"0004", 'name':'空调', 'brand':'格力', 'price':3900.00, 'count':0}, 13 {'id':"0005", 'name':'热水器', 'brand':'格力', 'price':688.00, 'count':30}, 14 {'id':"0006", 'name':'笔记本', 'brand':'联想', 'price':5699.00, 'count':10}, 15 {'id':"0007", 'name':'微波炉', 'brand':'苏泊尔', 'price':480.00, 'count':33}, 16 {'id':"0008", 'name':'投影仪', 'brand':'松下', 'price':1250.00, 'count':12}, 17 {'id':"0009", 'name':'吸尘器', 'brand':'飞利浦', 'price':999.00, 'count':9} 18 ] 19 20 #初始化购物车 21 products_cart = [] 22 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账 ') 23 while option in ['1','2','3']: 24 if option == '1': 25 #产品信息列表 26 print('产品和价格信息如下:') 27 print('*' * 60) 28 print(f"{'编号': <10} {'名称': <10} {'品牌': <10} {'价格': <10} {'库存数量': <10} ") 29 print('-' * 65) 30 for i in range(len(products)): 31 print(f"{products[i]['id']: <10} {products[i]['name']: <10} {products[i]['brand']: <10} {products[i]['price']: <10} {products[i]['count']: <10}") 32 print('-' * 65) 33 elif option == '2': 34 products_id = input('请输入您要购买的产品编号:') 35 while products_id not in [item['id'] for item in products]: 36 products_id = input('编号不存在,请重新输入您要购买的产品编号:') 37 38 count = int(input('请输入您要购买的产品数量:')) 39 while count > products[int(products_id) - 1]['count']: 40 count = int(input('数据超出库存,请重新输入您要购买的产品数量:')) 41 42 #将所购买商品加入购物车 43 if products_id not in [item['id'] for item in products_cart]: 44 products_cart.append({'id':products_id, 'count':count}) 45 else: 46 for i in range(len(products_cart)): 47 if products_cart[i].get('id') == products_id: 48 products_cart[i]['count'] += count 49 50 #更新商品列表 51 for i in range(len(products)): 52 if products[i]['id'] == products_id: 53 products[i]['count'] -= count 54 55 else: 56 print('购物车信息如下:') 57 print('*' * 60) 58 print(f"{'编号': <10} {'购买数量': <10}") 59 print('-' * 65) 60 for i in range(len(products_cart)): 61 print(f"{products_cart[i]['id']: <10} {products_cart[i]['count']: > 6}") 62 print('-' * 65) 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 if 5000 < amount <= 10000: 78 amount = amount * 0.95 79 elif 10000 < amount <= 20000: 80 amount = amount * 0.90 81 elif amount > 20000: 82 amount = amount * 0.85 83 else: 84 amount = amount * 1 85 print(f'购买成功,您需要支付{amount:.2f}元') 86 #退出系统 87 print('谢谢您的光临,下次再见!')
实验结果:

浙公网安备 33010602011771号