实验任务1
task1.py
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:整数范围是(0,100),可以取到100
问题2:利用 list(range(5)) 生成的有序序列范围是【0,4】的整数,不包括5
利用 list(range(1,5)) 生成的有序序列范围是【1,4】,不包括5
问题3:不一定是,因为取不到0值
问题4:一定是
实验任务2
task2_1.py
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.py
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.py
1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, 2 {'书名': '局外人', '作者': '阿尔贝.加缪'}, 3 {'书名': '设计中的设计', '作者': '原研哉'}, 4 {'书名': '万历十五年', '作者': '黄仁宇'}, 5 {'书名': '刀锋', '作者': '毛姆'} 6 ] 7 i=0 8 while i < len(book_infos): 9 d=book_infos[i] 10 for key,value in d.items(): 11 if key == '书名': 12 print(f'{i+1}.《{d[key]}》,',end='') 13 if key == '作者': 14 print(f'{d[key]}') 15 i+=1
实验任务3
task3.py
1 text="""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 """.lower() 22 chr_lst = [chr(i) for i in range(97,123)] 23 count = {} 24 for j in range(97,123): 25 count[chr(j)] = 0 26 for k in text: 27 if k in chr_lst: 28 count[k] += 1 29 30 lst = [] 31 for key in count: 32 lst.append([count[key],key]) 33 lst.sort(reverse = True) 34 35 i = 0 36 while i < len(lst): 37 print(f'{lst[i][1]}:{lst[i][0]}') 38 i += 1
实验任务4
task4.py
1 print('{:-^50}'.format('专业学号信息')) 2 code_majors={8326:'地信类', 3 8329:'计算机类', 4 8330:'气科类', 5 8336:'防灾工程', 6 8345:'海洋科学', 7 8382:'气象工程' 8 } 9 for key,value in code_majors.items(): 10 print(f'{key}:{value}') 11 print('{:-^50}'.format('学生专业查询')) 12 while True: 13 id = input('请输入学号:') 14 if id=='#': 15 print('查询结束...') 16 break 17 major = int(id[4:8]) 18 if major in code_majors.keys(): 19 print(f'专业是:{code_majors[major]}') 20 else: 21 print('不在这些专业中...')
实验任务5
task5.py
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)}')
实验任务6
task6.py
1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], 2 '2049777002': ['音乐', '旅行'], 3 '2049777003': ['马拉松', '健身', '游戏'], 4 '2049777004': [], 5 '2049777005': ['足球', '阅读'], 6 '2049777006': ['发呆', '闲逛'], 7 '2049777007': [], 8 '2049777008': ['书法', '电影'], 9 '2049777009': ['音乐', '阅读', '电影', '漫画'], 10 '2049777010': ['数学', '推理', '音乐', '旅行'] 11 } 12 13 l=[] 14 for v in datas.values(): 15 l.extend(v) 16 s=set(l) 17 18 d={} 19 for i in s: 20 d[i]=l.count(i) 21 22 l1=list(d.items()) 23 l2=[(v,k) for k,v in l1] 24 for v,k in sorted(l2,reverse=True): 25 print(f'{k}:{v}')
实验任务7
task7_1.py
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('**************************************************************') 31 print('%-10s'%'编号', '%-10s'%'名称', '%-10s' %'品牌', '%-10s'%' 价格', 32 '%-10s'%'库存数量') 33 print('--------------------------------------------------------------') 34 for i in range(len(products)): 35 print('%-10s'%products[i][0],'%-10s'%products[i][1],'%-10s'%products[i][2], 36 '%10.2f'%products[i][3],'%10d'%products[i][4]) 37 print('--------------------------------------------------------------') 38 elif option=='2': 39 product_id=input('请输入您要购买的产品编号:') 40 while product_id not in [item[0] for item in products]: 41 product_id=input('编号不存在,请重新输入您要购买的产品编号:') 42 43 count=int(input('请输入您要购买的产品数量:')) 44 while count>products[int(product_id)-1][4]: 45 count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) 46 47 #将所购买商品加入购物车 48 if product_id not in [item[0] for item in products_cart]: 49 products_cart.append([product_id,count]) 50 else: 51 for i in range(len(products_cart)): 52 if products_cart[i][0]==product_id: 53 products_cart[i][1]+=count 54 55 #更新商品列表 56 for i in range(len(products)): 57 if products[i][0]==product_id: 58 products[i][4]-=count 59 else: 60 print('购物车信息如下:') 61 print('******************************') 62 print('%-10s'%'编号','%-10s'%'购买数量') 63 print('------------------------------') 64 for i in range(len(products_cart)): 65 print('%-10s'%products_cart[i][0],'%6d'%products_cart[i][1]) 66 print('------------------------------') 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][0]==products_cart[i][0]: 76 product_index=j 77 break 78 price=products[product_index][3] 79 count=products_cart[i][1] 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('谢谢您的光临,下次再见!')
task7_2.py
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('**************************************************************') 31 print(f"{'编号':<10s}{'名称':<10s}{'品牌':<10s}{'价格':<10s}{'库存数量':<10s}") 32 print('--------------------------------------------------------------') 33 for i in range(len(products)): 34 print(f'{products[i][0]:<10s}{products[i][1]:<10s}{products[i][2]:<10s}{products[i][3]:<15.2f}{products[i][4]:<10d}') 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 #更新商品列表 54 for i in range(len(products)): 55 if products[i][0]==product_id: 56 products[i][4]-=count 57 else: 58 print('购物车信息如下:') 59 print('******************************') 60 print(f"{'编号':<10s}{'购买数量':<10s}") 61 print('------------------------------') 62 for i in range(len(products_cart)): 63 print(f'{products_cart[i][0]:<15s}{products_cart[i][1]:<6d}') 64 print('------------------------------') 65 option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 66 67 #计算金额 68 if len(products_cart)>0: 69 amount=0 70 for i in range(len(products_cart)): 71 product_index=0 72 for j in range(len(products)): 73 if products[j][0]==products_cart[i][0]: 74 product_index=j 75 break 76 price=products[product_index][3] 77 count=products_cart[i][1] 78 amount+=price*count 79 80 if 5000<amount<=10000: 81 amount=amount*0.95 82 elif 10000<amount<=20000: 83 amount=amount*0.90 84 elif amount>20000: 85 amount=amount*0.85 86 else: 87 amount=amount*1 88 print(f'购买成功,您需要支付{amount:<8.2f}元') 89 90 #退出系统 91 print('谢谢您的光临,下次再见!')
实验任务8
task8_1.py
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'%' 价格', 32 '%-10s'%'库存数量') 33 print('--------------------------------------------------------------') 34 for i in range(len(products)): 35 print('%-10s'%products[i]['id'],'%-10s'%products[i]['name'], 36 '%-10s'%products[i]['brand'],'%10.2f'%products[i]['price'], 37 '%10d'%products[i]['count']) 38 print('--------------------------------------------------------------') 39 elif option=='2': 40 product_id=input('请输入您要购买的产品编号:') 41 while product_id not in [item['id'] for item in products]: 42 product_id=input('编号不存在,请重新输入您要购买的产品编号:') 43 44 count=int(input('请输入您要购买的产品数量:')) 45 while count>products[int(product_id)-1]['count']: 46 count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) 47 48 #将所购买商品加入购物车 49 if product_id not in [item['id'] for item in products_cart]: 50 products_cart.append({'id':product_id,'count':count}) 51 else: 52 for i in range(len(products_cart)): 53 if products_cart[i].get('id')==product_id: 54 products_cart[i]['count']+=count 55 56 #更新商品列表 57 for i in range(len(products)): 58 if products[i]['id']==product_id: 59 products[i]['count']-=count 60 else: 61 print('购物车信息如下:') 62 print('******************************') 63 print('%-10s'%'编号','%-10s'%'购买数量') 64 print('------------------------------') 65 for i in range(len(products_cart)): 66 print('%-10s'%products_cart[i]['id'],'%6d'%products_cart[i]['count']) 67 print('------------------------------') 68 option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 69 70 #计算金额 71 if len(products_cart)>0: 72 amount=0 73 for i in range(len(products_cart)): 74 product_index=0 75 for j in range(len(products)): 76 if products[j]['id']==products_cart[i]['id']: 77 product_index=j 78 break 79 price=products[product_index]['price'] 80 count=products_cart[i]['count'] 81 amount+=price*count 82 83 if 5000<amount<=10000: 84 amount=amount*0.95 85 elif 10000<amount<=20000: 86 amount=amount*0.90 87 elif amount>20000: 88 amount=amount*0.85 89 else: 90 amount=amount*1 91 print('购买成功,您需要支付%8.2f元'%amount) 92 93 #退出系统 94 print('谢谢您的光临,下次再见!')
task8_2.py
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}{products[i]['price']:<15.2f}{products[i]['count']:<10d}") 35 print('--------------------------------------------------------------') 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 41 count=int(input('请输入您要购买的产品数量:')) 42 while count>products[int(product_id)-1]['count']: 43 count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) 44 45 #将所购买商品加入购物车 46 if product_id not in [item['id'] for item in products_cart]: 47 products_cart.append({'id':product_id,'count':count}) 48 else: 49 for i in range(len(products_cart)): 50 if products_cart[i].get('id')==product_id: 51 products_cart[i]['count']+=count 52 53 #更新商品列表 54 for i in range(len(products)): 55 if products[i]['id']==product_id: 56 products[i]['count']-=count 57 else: 58 print('购物车信息如下:') 59 print('******************************') 60 print(f"{'编号':<15s}{'购买数量':<6s}") 61 print('------------------------------') 62 for i in range(len(products_cart)): 63 print(f"{products_cart[i]['id']:<10s}{products_cart[i]['count']:<6d}") 64 print('------------------------------') 65 option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') 66 67 #计算金额 68 if len(products_cart)>0: 69 amount=0 70 for i in range(len(products_cart)): 71 product_index=0 72 for j in range(len(products)): 73 if products[j]['id']==products_cart[i]['id']: 74 product_index=j 75 break 76 price=products[product_index]['price'] 77 count=products_cart[i]['count'] 78 amount+=price*count 79 80 if 5000<amount<=10000: 81 amount=amount*0.95 82 elif 10000<amount<=20000: 83 amount=amount*0.90 84 elif amount>20000: 85 amount=amount*0.85 86 else: 87 amount=amount*1 88 print(f'购买成功,您需要支付{amount:<8.2f}元') 89 90 #退出系统 91 print('谢谢您的光临,下次再见!')
标签:语句,count,product,编程,数据类型,cart,products,print,id From: https://www.cnblogs.com/xq040718/p/17332499.html