实验任务1
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 # 遍历key-value对:实现方式1 10 for key, value in book_info.items(): 11 print(f'{key}: {value}') 12 print() 13 14 # 遍历key-value对:实现方式2 15 for item in book_info.items(): 16 print(f'{item[0]}: {item[1]}') 17 print() 18 19 # 遍历值:实现方式1 20 for value in book_info.values(): 21 print(value, end = ' ') 22 print() 23 24 # 遍历值: 实现方式2 25 for key in book_info.keys(): 26 print(book_info[key], end = ' ')
task2_3.py
1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, 2 {'书名': '局外人', '作者': '阿尔贝.加缪'}, 3 {'书名': '设计中的设计', '作者': '原研哉'}, 4 {'书名': '万历十五年', '作者': '黄仁宇'}, 5 {'书名': '刀锋', '作者': '毛姆'} 6 ] 7 for i in range(len(book_infos)): 8 l=[v for v in book_infos[i].values()] 9 print(f'{i+1}.《{l[0]}》,{l[1]}')
实验任务3
task3.py
text='''The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!''' l=[text.lower().count(chr(i)) for i in range(97,97+26)] d={} for j in range(26): d.update({chr(j+97):l[j]}) l1=list(d.items()) l2=[(v,k) for k,v in l1] for v,k in sorted(l2,reverse=True): print(f'{k}:{v}')
实验任务4
task4.py
code_majors={8326:'地信类', 8329:'计算机类', 8330:'气科类', 8336:'防灾工程', 8345:'海洋科学', 8382:'气象工程'} print(f'{"专业代号信息":-^50s}') for k,v in code_majors.items(): print(f'{k}:{v}') print(f'{"学生专业查询":-^50s}') n=input('请输入学号:') while n!='#': if int(n[4:8]) in code_majors: print(f'专业是:{code_majors.get(int(n[4:8]))}') else: print('不在这些专业中...') n=input('请输入学号:') print('查询结束...')
实验任务5
task5.py
import random lucky_day=random.randint(1,31) print(f'猜猜2023年5月哪一天会是你的lucky day{chr(0x1f609)}') d=eval(input('你有三次机会,猜吧(1~31):')) for i in range(3): if d<1 or d>31: print('地球上没有这一天啦,你是外星人吗') elif d<lucky_day: print('猜早啦,你的lucky day还没到呢') elif d>lucky_day: print('猜晚啦,你的lucky day已经过啦') else: print(f'哇,猜中了{chr(0x1f923)}') break if i==2: break d=eval(input('再猜(1~31):')) if d!=lucky_day: print('哇哦,次数用光啦.') print(f'偷偷告诉你,5月你的lucky day是{lucky_day}号.good luck{chr(0x1f60a)}')
实验任务6
task6.py
datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], '2049777002': ['音乐', '旅行'], '2049777003': ['马拉松', '健身', '游戏'], '2049777004': [], '2049777005': ['足球', '阅读'], '2049777006': ['发呆', '闲逛'], '2049777007': [], '2049777008': ['书法', '电影'], '2049777009': ['音乐', '阅读', '电影', '漫画'], '2049777010': ['数学', '推理', '音乐', '旅行'] } l=[] for v in datas.values(): l.extend(v) s=set(l) d={} for i in s: d[i]=l.count(i) l1=list(d.items()) l2=[(v,k) for k,v in l1] for v,k in sorted(l2,reverse=True): print(f'{k}:{v}')
实验任务7
task7.1.py
""" 家用电器销售系统 v1.3 """ #欢迎信息 print('欢迎使用家用电器销售系统!') #商品数据初始化 products=[ ['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(products)): print('%-10s'%products[i][0],'%-10s'%products[i][1],'%-10s'%products[i][2], '%10.2f'%products[i][3],'%10d'%products[i][4]) print('--------------------------------------------------------------') elif option=='2': product_id=input('请输入您要购买的产品编号:') while product_id not in [item[0] for item in products]: product_id=input('编号不存在,请重新输入您要购买的产品编号:') count=int(input('请输入您要购买的产品数量:')) while count>products[int(product_id)-1][4]: count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) #将所购买商品加入购物车 if product_id not in [item[0] for item in products_cart]: products_cart.append([product_id,count]) else: for i in range(len(products_cart)): if products_cart[i][0]==product_id: products_cart[i][1]+=count #更新商品列表 for i in range(len(products)): if products[i][0]==product_id: products[i][4]-=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)): product_index=0 for j in range(len(products)): if products[j][0]==products_cart[i][0]: product_index=j break price=products[product_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.90 elif amount>20000: amount=amount*0.85 else: amount=amount*1 print('购买成功,您需要支付%8.2f元'%amount) #退出系统 print('谢谢您的光临,下次再见!')
task7.2.py
""" 家用电器销售系统 v1.3 """ #欢迎信息 print('欢迎使用家用电器销售系统!') #商品数据初始化 products=[ ['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(f"{'编号':<10s}{'名称':<10s}{'品牌':<10s}{'价格':<10s}{'库存数量':<10s}") print('--------------------------------------------------------------') for i in range(len(products)): print(f'{products[i][0]:<10s}{products[i][1]:<10s}{products[i][2]:<10s}{products[i][3]:<15.2f}{products[i][4]:<10d}') print('--------------------------------------------------------------') elif option=='2': product_id=input('请输入您要购买的产品编号:') while product_id not in [item[0] for item in products]: product_id=input('编号不存在,请重新输入您要购买的产品编号:') count=int(input('请输入您要购买的产品数量:')) while count>products[int(product_id)-1][4]: count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) #将所购买商品加入购物车 if product_id not in [item[0] for item in products_cart]: products_cart.append([product_id,count]) else: for i in range(len(products_cart)): if products_cart[i][0]==product_id: products_cart[i][1]+=count #更新商品列表 for i in range(len(products)): if products[i][0]==product_id: products[i][4]-=count else: print('购物车信息如下:') print('******************************') print(f"{'编号':<10s}{'购买数量':<10s}") print('------------------------------') for i in range(len(products_cart)): print(f'{products_cart[i][0]:<15s}{products_cart[i][1]:<6d}') print('------------------------------') option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') #计算金额 if len(products_cart)>0: amount=0 for i in range(len(products_cart)): product_index=0 for j in range(len(products)): if products[j][0]==products_cart[i][0]: product_index=j break price=products[product_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.90 elif amount>20000: amount=amount*0.85 else: amount=amount*1 print(f'购买成功,您需要支付{amount:<8.2f}元') #退出系统 print('谢谢您的光临,下次再见!')
实验任务8
task8.1.py
""" 家用电器销售系统 v1.4 """ #欢迎信息 print('欢迎使用家用电器销售系统!') #商品数据初始化 products=[ {'id':'0001','name':'电视机','brand':'海尔','price':5999.00,'count':20}, {'id':'0002','name':'冰箱','brand':'西门子','price':6998.00,'count':15}, {'id':'0003','name':'洗衣机','brand':'小天鹅','price':1999.00,'count':10}, {'id':'0004','name':'空调','brand':'格力','price':3900.00,'count':0}, {'id':'0005','name':'热水器','brand':'格力','price':688.00,'count':30}, {'id':'0006','name':'笔记本','brand':'联想','price':5699.00,'count':10}, {'id':'0007','name':'微波炉','brand':'苏泊尔','price':480.00,'count':33}, {'id':'0008','name':'投影仪','brand':'松下','price':1250.00,'count':12}, {'id':'0009','name':'吸尘器','brand':'飞利浦','price':999.00,'count':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(products)): print('%-10s'%products[i]['id'],'%-10s'%products[i]['name'], '%-10s'%products[i]['brand'],'%10.2f'%products[i]['price'], '%10d'%products[i]['count']) print('--------------------------------------------------------------') elif option=='2': product_id=input('请输入您要购买的产品编号:') while product_id not in [item['id'] for item in products]: product_id=input('编号不存在,请重新输入您要购买的产品编号:') count=int(input('请输入您要购买的产品数量:')) while count>products[int(product_id)-1]['count']: count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) #将所购买商品加入购物车 if product_id not in [item['id'] for item in products_cart]: products_cart.append({'id':product_id,'count':count}) else: for i in range(len(products_cart)): if products_cart[i].get('id')==product_id: products_cart[i]['count']+=count #更新商品列表 for i in range(len(products)): if products[i]['id']==product_id: products[i]['count']-=count else: print('购物车信息如下:') print('******************************') print('%-10s'%'编号','%-10s'%'购买数量') print('------------------------------') for i in range(len(products_cart)): print('%-10s'%products_cart[i]['id'],'%6d'%products_cart[i]['count']) print('------------------------------') option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') #计算金额 if len(products_cart)>0: amount=0 for i in range(len(products_cart)): product_index=0 for j in range(len(products)): if products[j]['id']==products_cart[i]['id']: product_index=j break price=products[product_index]['price'] count=products_cart[i]['count'] amount+=price*count if 5000<amount<=10000: amount=amount*0.95 elif 10000<amount<=20000: amount=amount*0.90 elif amount>20000: amount=amount*0.85 else: amount=amount*1 print('购买成功,您需要支付%8.2f元'%amount) #退出系统 print('谢谢您的光临,下次再见!')
task8.2.py
""" 家用电器销售系统 v1.4 """ #欢迎信息 print('欢迎使用家用电器销售系统!') #商品数据初始化 products=[ {'id':'0001','name':'电视机','brand':'海尔','price':5999.00,'count':20}, {'id':'0002','name':'冰箱','brand':'西门子','price':6998.00,'count':15}, {'id':'0003','name':'洗衣机','brand':'小天鹅','price':1999.00,'count':10}, {'id':'0004','name':'空调','brand':'格力','price':3900.00,'count':0}, {'id':'0005','name':'热水器','brand':'格力','price':688.00,'count':30}, {'id':'0006','name':'笔记本','brand':'联想','price':5699.00,'count':10}, {'id':'0007','name':'微波炉','brand':'苏泊尔','price':480.00,'count':33}, {'id':'0008','name':'投影仪','brand':'松下','price':1250.00,'count':12}, {'id':'0009','name':'吸尘器','brand':'飞利浦','price':999.00,'count':9}, ] #初始化用户购物车 products_cart=[] option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') while option in['1','2','3']: if option=='1': #产品信息列表 print('产品和价格信息如下:') print('**************************************************************') print(f"{'编号':<10s}{'名称':<10s}{'品牌':<10s}{'价格':<10s}{'库存数量':<10s}") print('--------------------------------------------------------------') for i in range(len(products)): print(f"{products[i]['id']:<10s}{products[i]['name']:<10s}{products[i]['brand']:<10s}{products[i]['price']:<15.2f}{products[i]['count']:<10d}") print('--------------------------------------------------------------') elif option=='2': product_id=input('请输入您要购买的产品编号:') while product_id not in [item['id'] for item in products]: product_id=input('编号不存在,请重新输入您要购买的产品编号:') count=int(input('请输入您要购买的产品数量:')) while count>products[int(product_id)-1]['count']: count=int(input('数量超出库存,请重新输入您要购买的产品数量:')) #将所购买商品加入购物车 if product_id not in [item['id'] for item in products_cart]: products_cart.append({'id':product_id,'count':count}) else: for i in range(len(products_cart)): if products_cart[i].get('id')==product_id: products_cart[i]['count']+=count #更新商品列表 for i in range(len(products)): if products[i]['id']==product_id: products[i]['count']-=count else: print('购物车信息如下:') print('******************************') print(f"{'编号':<15s}{'购买数量':<6s}") print('------------------------------') for i in range(len(products_cart)): print(f"{products_cart[i]['id']:<10s}{products_cart[i]['count']:<6d}") print('------------------------------') option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') #计算金额 if len(products_cart)>0: amount=0 for i in range(len(products_cart)): product_index=0 for j in range(len(products)): if products[j]['id']==products_cart[i]['id']: product_index=j break price=products[product_index]['price'] count=products_cart[i]['count'] amount+=price*count if 5000<amount<=10000: amount=amount*0.95 elif 10000<amount<=20000: amount=amount*0.90 elif amount>20000: amount=amount*0.85 else: amount=amount*1 print(f'购买成功,您需要支付{amount:<8.2f}元') #退出系统 print('谢谢您的光临,下次再见!')
实验总结:
1. 知道Python中组合数据类型字符串(str)、列表(list)、元组(tuple)、集合(set)、字典(dict)的表示、特性。 2. 能够正确使用字符串(str)、列表(list)、元组(tuple)、集合(set)、字典(dict)的常用操作。 3. 针对具体问题场景,灵活、组合使用多种数据类型,应用或设计算法,使用控制语句编程解决实际问题。标签:语句,count,product,编程,数据类型,cart,products,print,id From: https://www.cnblogs.com/fengguowanyueyi/p/17343232.html