task1
问题1:[0,100],可以取到
问题2:{0,1,2,3,4}, 不包括:{1,2,3,4},不包括
问题3:不一定是,集合中不存在重复元素,元素个数在5以内,具体个数不一定
问题4:一定
task2-1
# 列表遍历 lst = [55, 92, 88, 79, 96] # 遍历方式1: 使用while + 索引 i = 0 while i < len(lst): print(lst[i], end = ' ') i += 1 print() # 遍历方式2:使用for + 索引 for i in range(len(lst)): print(lst[i], end = ' ') print() # 遍历方式3: 使用for...in for i in lst: print(i , end = ' ') print()
task2-2
# 字典遍历 book_info = {'isbn': '978-7-5356-8297-0', '书名': '白鲸记', '作者': '克里斯多夫.夏布特', '译者': '高文婧', '出版社': '湖南美术出版社', '售价': 82 } # 遍历key-value对:实现方式1 for key, value in book_info.items(): print(f'{key}: {value}') print() # 遍历key-value对:实现方式2 for item in book_info.items(): print(f'{item[0]}: {item[1]}') print() # 遍历值:实现方式1 for value in book_info.values(): print(value, end = ' ') print() # 遍历值: 实现方式2 for key in book_info.keys(): print(book_info[key], end = ' ')
task2-3
book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'}, {'书名': '局外人', '作者': '阿尔贝.加缪'}, {'书名': '设计中的设计', '作者': '原研哉'}, {'书名': '万历十五年', '作者': '黄仁宇'}, {'书名': '刀锋', '作者': '毛姆'} ] for i in range(len(book_infos)): k=list(book_infos[i].values()) print(f'{i+1}. 《{k[0]}》,{k[1]}')
task3
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!''' d={} text=text.lower() for i in range(97,97+26): d[chr(i)]=0 for j in range(len(text)): if text[j] in d: d[text[j]]+=1 list1=[] for n in d: a=(d.get(n),n) list1.append(a) list1.sort(reverse=True) for h in list1: print(f'{h[1]}:{h[0]}')
task4
print('{:-^50}'.format('专业代号')) code_majors={8326:'地信类', 8329 :'计算机类', 8330 :'气科类', 8336 :'防灾工程', 8345 :'海洋科学', 8382 :'气象工程'} for i in code_majors: print(f'{i}:{code_majors[i]}') print('{:-^50}'.format('学生专业查询')) x=0 while x<1: s=input('请输入学号:') if s=='#': x=10 else: s=int(s[4:8]) if s in code_majors: print(f'专业是:{code_majors[s]}') else: print('不在这些专业中......')
task5
import random x=random.randint(1,31) g=3 print('你有三次机会猜出5月的lucky day\U0001F61B') while g>0: if g==3: s=int(input('来猜吧(1~31)\U0001f92A:')) elif g==2: s=int(input('再猜(1~31)\U0001f92A:')) else: s=int(input('最后一次(1~31)\U0001f92A:')) if s in range(1,32) and s>x: print('晚了,你的lucky day 已过\U0001FA78') g-=1 elif s in range(1,32) and s<x: print('早了,你的lucky day 还没到\U0001F4A9') g-=1 elif s==x: print('中了,\U0001f607') g=-1 else: print('地球人不过这一天,你是外星人吗?\U0001f6f8') g-=1 if g==0: print(f'GAME OVER 你的lucky day 是{x}日\U0001f9cf')
task6
datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], '2049777002': ['音乐', '旅行'], '2049777003': ['马拉松', '健身', '游戏'], '2049777004': [], '2049777005': ['足球', '阅读'], '2049777006': ['发呆', '闲逛'], '2049777007': [], '2049777008': ['书法', '电影'], '2049777009': ['音乐', '阅读', '电影', '漫画'], '2049777010': ['数学', '推理', '音乐', '旅行'] } d={} for i in range(2049777001,2049777011): for j in range(len(datas[str(i)])): d[datas[str(i)][j]]=d.get(datas[str(i)][j],0)+1 y=list(d.items()) z=[] for j in y: z.append((j[1],j[0])) z.sort(reverse=True) for k in z: print(f'{k[1]}:{k[0]}')
task7
# 欢迎信息 print('欢迎使用家用电器销售系统') # 商品数据初始化 pro = [ ['0001','电视机','海尔',5999.00,20], ['0002','冰箱','西门子',6998.00,15], ['0003','洗衣机','小天鹅',1999.00,10], ['0004','空调','格力',3900.00,0], ['0005','热水器','格力',680.00,30], ['0006','笔记本','联想',5699.00,10], ['0007','微波炉','苏泊尔',480.00,33], ['0008','投影仪','松下',1250.00,12], ['0009','吸尘器','飞利浦',999.00,9], ] #初始化用户购物车 pro_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 pro_cart]: pro_cart.append([pro_id,count]) else: for i in range(len(pro_cart)): if pro_cart[i][0]==pro_id: pro_cart[i][1]+=count #更新商品列表 for i in range(len(pro)): if pro[i][0] == pro_id: pro[i][4] -= count else: print('购物车信息如下:') print('************************************') print('%-10s'%'编号','%-10s'%'购买数量') print('------------------------------------') for i in range(len(pro_cart)): print('%-10s'%pro_cart[i][0],'%6d'%pro_cart[i][1]) print('------------------------------------') option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。') # 计算金额 if len(pro_cart)>0: amount=0 for i in range(len(pro_cart)): pro_index=0 for j in range(len(pro)): if pro[j][0]==pro_cart[i][0]: pro_index=j break price=pro[pro_index][3] count=pro_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('谢谢您的光临,下次再见!')
# 欢迎信息 print('欢迎使用家用电器销售系统') # 商品数据初始化 pro = [ ['0001','电视机','海尔',5999.00,20], ['0002','冰箱','西门子',6998.00,15], ['0003','洗衣机','小天鹅',1999.00,10], ['0004','空调','格力',3900.00,0], ['0005','热水器','格力',680.00,30], ['0006','笔记本','联想',5699.00,10], ['0007','微波炉','苏泊尔',480.00,33], ['0008','投影仪','松下',1250.00,12], ['0009','吸尘器','飞利浦',999.00,9], ] #初始化用户购物车 pro_cart=[] option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') while option in['1','2','3',]: if option=='1': print('产品和价格信息如下') print('{:*^60}'.format('*')) print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % '价格', '%-10s' % '库存数量') print(':-^60'.format('-')) 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('{:-^60}'.format('-')) 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 pro_cart]: pro_cart.append([pro_id,count]) else: for i in range(len(pro_cart)): if pro_cart[i][0]==pro_id: pro_cart[i][1]+=count #更新商品列表 for i in range(len(pro)): if pro[i][0] == pro_id: pro[i][4] -= count else: print('购物车信息如下:') print('{:*^60}'.format('*')) print('%-10s'%'编号','%-10s'%'购买数量') print('{:-^60}'.format('-')) for i in range(len(pro_cart)): print('%-10s'%pro_cart[i][0],'%6d'%pro_cart[i][1]) print('{:-^40}'.format('-')) option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。') # 计算金额 if len(pro_cart)>0: amount=0 for i in range(len(pro_cart)): pro_index=0 for j in range(len(pro)): if pro[j][0]==pro_cart[i][0]: pro_index=j break price=pro[pro_index][3] count=pro_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('谢谢您的光临,下次再见!')
task8
print('欢迎使用家用电器销售系统') # 商品数据初始化 pros =[ {'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':680.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}, ] #初始化用户购物车 pros_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(pros)): print('%-10s' %pros[i]['id'],'%-10s' %pros[i]['name'], '%-10s' %pros[i]['brand'],'%-10.2f' %pros[i]['price'], '%10d' %pros[i]['count']) print('------------------------------------------------------------') elif option == '2': pro_id = input('请输入您要购买的产品编号:') while pro_id not in [item['id'] for item in pros]: pro_id = input('编号不存在,请重新输入您要购买的产品编号:') count = int(input('请输入您要购买的产品数量:')) while count > pros[int(pro_id) - 1]['count']: count = int(input('数量超出库存,请重新输入您要购买的产品数量:')) # 将所购买商品加入购物车 if pro_id not in [item['id'] for item in pros_cart]: pros_cart.append({'id':pro_id,'count':count}) else: for i in range(len(pro_cart)): if pros_cart[i].get('id') == product_id: pros_cart[i]['count'] += count # 更新商品列表 for i in range(len(pros)): if pros[i]['id'] == pro_id: pros[i]['count'] -= count else: print('购物车信息如下:') print('************************************') print('%-10s'%'编号','%-10s'%'购买数量') print('------------------------------------') for i in range(len(pros_cart)): print('%-10s' %pros_cart[i]['id'],'%6d' %pros_cart[i]['count']) print('------------------------------------') option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') # 计算金额 if len(pros_cart) > 0: amount = 0 for i in range(len(pros_cart)): pro_index = 0 for j in range(len(pros)): if pros[j]['id'] == pros_cart[i]['id']: pro_index = j break price = pros[pro_index]['price'] count = pros_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('谢谢您的光临,下次再见!')
print('欢迎使用家用电器销售系统') # 商品数据初始化 pros =[ {'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':680.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}, ] #初始化用户购物车 pros_cart = [] option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') while option in ['1','2','3']: if option == '1': # 产品信息列表 print('产品和价格信息如下:') print(f'{"":*^60}') print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % '价格', '%-10s' % '库存数量') print(f'{"":-^60}') for i in range(len(pros)): print('%-10s' %pros[i]['id'],'%-10s' %pros[i]['name'], '%-10s' %pros[i]['brand'],'%-10.2f' %pros[i]['price'], '%10d' %pros[i]['count']) print(f'{"":-^60}') elif option == '2': pro_id = input('请输入您要购买的产品编号:') while pro_id not in [item['id'] for item in pros]: pro_id = input('编号不存在,请重新输入您要购买的产品编号:') count = int(input('请输入您要购买的产品数量:')) while count > pros[int(pro_id) - 1]['count']: count = int(input('数量超出库存,请重新输入您要购买的产品数量:')) # 将所购买商品加入购物车 if pro_id not in [item['id'] for item in pros_cart]: pros_cart.append({'id':pro_id,'count':count}) else: for i in range(len(pro_cart)): if pros_cart[i].get('id') == product_id: pros_cart[i]['count'] += count # 更新商品列表 for i in range(len(pros)): if pros[i]['id'] == pro_id: pros[i]['count'] -= count else: print('购物车信息如下:') print(f'{"":-^60}') print('%-10s'%'编号','%-10s'%'购买数量') print(f'{"":-^60}') for i in range(len(pros_cart)): print('%-10s' %pros_cart[i]['id'],'%6d' %pros_cart[i]['count']) print(f'{"":-^60}') option = input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账') # 计算金额 if len(pros_cart) > 0: amount = 0 for i in range(len(pros_cart)): pro_index = 0 for j in range(len(pros)): if pros[j]['id'] == pros_cart[i]['id']: pro_index = j break price = pros[pro_index]['price'] count = pros_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('谢谢您的光临,下次再见!')
标签:count,pro,pros,cart,实验,print,id From: https://www.cnblogs.com/-bug/p/17343283.html