实验3控制语句与组合数据类型应用编程
实验任务一
实验源码
import random print('用列表存储随机整数: ') lst = [random.randint(0, 100) for i in range(5)] print(lst) print('\n用集合存储随机整数: ') s1 = {random.randint(0, 100) for i in range(5)} print(s1) print('\n用集合存储随机整数: ') s2 = set() while len(s2) < 5: s2.add(random.randint(0, 100)) print(s2)
运行测试截图

问题1:random.randint(0,100)生成的随机整数范围是?能否取到100?
答:范围是0~100,其中包括100
问题2:利用list(range(5)),list(range(1,5))生成的有序序列范围是?是否包括5?
答:list(range(5)的有序序列范围是0~4,其中不包括5;list(range(1,5))生成的有序序列范围是1~4,其中不包括5、
问题3:使用line代码生成的集合s1,len(s1)一定是5吗?如果不是请解释原因。
答:不一定是5,若随机生成两个相同的数,但集合中只保留一个。
问题4:使用line12-14 生成的集合s2,len(s2)一定是5嘛?如果不是请解释原因。
答:一定是5
实验任务2-1
实验源码
1st = [55, 92, 88, 79, 96] i = 0 while i < len(1st): print(1st[i], end = ' ') i += 1 print() for i in range(len(1st)): print() for i in 1st: print(i , end =' ') print()
运行测试截图

实验任务2-2
实验源码
book_info = {'isbn': '978-7-5356-8297-0',
'书名': '白鲸记',
'作者': '克里斯多夫.夏布寺',
'译者': '高文婧',
'出版社': '湖南美术出版社',
'售价': 82
}
for key , value in book_info.items():
print(f'{key}: {value}')
print()
for item in book_info.items():
print(f'{item[0]}: {item[1]}')
print()
for value in book_info.values():
print(value, end = ' ')
print()
for key in book_info.keys():
print(book_info[key], end = ' ')
运行测试截图

实验任务2-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 zen = '''The Zen of Python, by Tim Peters 2 Beautiful is better than ugly. 3 Explicit is better than implicit. 4 Simple is better than complex. 5 Complex is better than complicated. 6 Flat is better than nested. 7 Sparse is better than dense. 8 Readability counts. 9 Special cases aren't special enough to break the rules. 10 Although practicality beats purity. 11 Errors should never pass silently. 12 Unless explicitly silenced. 13 In the face of ambiguity, refuse the temptation to guess. 14 There should be one-- and preferably only one --obvious way to do it. 15 Although that way may not be obvious at first unless you're Dutch. 16 Now is better than never. 17 Although never is often better than *right* now. 18 If the implementation is hard to explain, it's a bad idea. 19 If the implementation is easy to explain, it may be a good idea. 20 Namespaces are one honking great idea -- let's do more of those! 21 ''' 22 zen = zen.lower() 23 letter_sum = dict() 24 for i in range(97,97+26): 25 letter = chr(i) 26 sum1 = zen.count(letter) 27 letter_sum[letter]=sum1 28 letter_sum = sorted(letter_sum.items(),key=lambda x:x[1],reverse = True) 29 for key, value in dict(letter_sum).items(): 30 print(f'{key}: {value}')
运行测试截图

实验任务4
实验源码
1 print('-'*22,'专业代码信息','-'*22) 2 code_majors = {'8326':'地信类','8329':'计算机类','8330':'气科类','8336':'防灾工程','8345':'海洋科学','8382':'气象工程'} 3 for key,value in code_majors.items(): 4 print(f'{key}:{value}') 5 print('-'*22,'学生专业查询','-'*22) 6 while True: 7 numbers = input('请输入学号:') 8 if numbers == '#': 9 print('查询结束') 10 break 11 keys = list(code_majors.keys()) 12 if numbers[4:8] in keys: 13 print(f'专业是:{code_majors[numbers[4:8]]}') 14 else:print('不在这些专业里')
运行测试截图

实验任务5
实验源码
1 import random 2 print('猜猜2023年5月哪一天会是你的lucky day','🤣') 3 x=random.randint(1,31) 4 date=int(input('你有三次机会,猜吧(1~31):')) 5 count=1 6 if date==x: 7 print('哇,猜中了','😊') 8 else: 9 while count<3: 10 count+=1 11 if not(1<=date<=31): 12 print('地球上没有这一天啦,你是外星人吗') 13 date=int(input('再猜(1~31):')) 14 elif date<x: 15 print('猜早了,你的luchy day还没到呢') 16 date=int(input('再猜(1~31):')) 17 elif date>x: 18 print('猜晚了,你的lucky day已经过了') 19 date=int(input('再猜(1~31):')) 20 else: 21 print('哇,猜中了','😊') 22 count +=1 23 break 24 if count==3 and x !=date: 25 print('哇哦,次数用光了') 26 print(f'偷偷告诉你,5月你的lucky day是{x}号.good luck\U0001F60A') 27 elif count==3 and x ==date: 28 print('哇,猜中了','😁')
运行测试截图

实验任务6
实验源码
1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], 2 '2049777002': ['音乐', '旅行'], 3 '2049777003': ['马拉松', '健身', '游戏'], 4 '2049777004': [], 5 '2049777005': ['足球', '阅读'], 6 '2049777006': ['发呆', '闲逛'], 7 '2049777007': [], 8 '2049777008': ['书法', '电影'], 9 '2049777009': ['音乐', '阅读', '电影', '漫画'], 10 '2049777010': ['数学', '推理', '音乐', '旅行'] 11 } 12 hobby = {} 13 for value in list(datas.values()): 14 for i in value: 15 hobby[i] = hobby.get(i, 0) + 1 16 for j in hobby.items(): 17 print(f'{j[0]}:{j[1]}')
运行测试截图

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

实验任务7-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('%-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('谢谢您的光临,下次再见!')
运行测试截图

实验任务8-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('谢谢您的光临,下次再见!')
运行测试截图

实验任务8-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('谢谢您的光临,下次再见!')
运行测试截图


浙公网安备 33010602011771号