首页 > 编程语言 >实验三 控制语句与组合数据类型应用编程

实验三 控制语句与组合数据类型应用编程

时间:2023-04-22 21:16:11浏览次数:36  
标签:语句 count pro 编程 数据类型 cart products print id

实验任务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)

运行截图

实验任务2

task2-1

源代码

 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 print()
10 
11 #遍历方式2:使用for+索引
12 for i in range(len(lst)):
13     print(lst[i],end=' ')
14 print()
15 
16 #遍历方式3:使用for...in
17 for i in lst:
18     print(i,end=' ')
19 print()

运行截图

task2-2

源代码

 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 #实现方式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=' ')
27 print()

运行截图

task2-3

源代码

 1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
 2              {'书名': '局外人', '作者': '阿尔贝.加缪'},
 3              {'书名': '设计中的设计', '作者': '原研哉'},
 4              {'书名': '万历十五年', '作者': '黄仁宇'},
 5              {'书名': '刀锋', '作者': '毛姆'}
 6              ]
 7 for i in range(len(book_infos)):
 8     d=book_infos[i]
 9     for key,value in d.items():
10         if key =='书名':
11             print(f'{i+1}.《{d[key]}》,',end='')
12         if key =='作者':
13             print(f'{d[key]}')

运行截图

实验任务3

源代码

 1 text='''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.lst
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 x=text.lower()
23 y='abcdefghijklmnopqrstuvwxyz'
24 z={}
25 for i in y:
26     if i in x:
27         z.update({i:x.count(i)})
28 lst1=list(z.items())
29 lst2=[(value,key) for key,value in lst1]
30 for value,key in sorted(lst2,reverse=True):
31     print(f'{key}:{value}')

运行截图 

 实验任务4

源代码

 1 tittle='专业代号信息'
 2 print(tittle.center(50,'-'))
 3 code_majors={8326:'地信类',8329:'计算机类',8330:'气科类',8336:'防灾工程',8345:'海洋科学',8382:'气象工程'}
 4 for key,value in code_majors.items():
 5     print(f'{key}:{value}')
 6 tittle2='学生专业查询'
 7 print(tittle2.center(50,'-'))
 8 while True:
 9     x=input('请输入学号:')
10     if x=='#':
11         print('查询结束')
12         break
13     y=int(x[4:8])
14     if y in code_majors.keys():
15         print(f'专业是:{code_majors[y]}')
16     else:
17         print('不在这些专业中')

运行截图

实验任务5

源代码

 1 import random
 2 lucky_day=random.randint(1,31)
 3 print('猜猜2023年5月哪一天会是你的lucky day')
 4 day=int(input('你有三次机会,猜吧(1~31):'))
 5 i=0
 6 while i<2:
 7     if day<1 or day>31:
 8         print('地球上没有这一天,你是外星人吗?')
 9         day=int(input('再猜:'))
10         i+=1
11     if day<lucky_day:
12         print('猜早啦,你的lucky day还没到呢')
13         day=int(input('再猜:'))
14         i+=1
15         if day>lucky_day:
16             print('猜晚啦,你的lucky day已经过去啦')
17             day=int(input('再猜:'))
18             i+=1
19             if day==lucky_day:
20                 print('哇哦,猜中啦')
21                 i+=1
22 if day!=lucky_day:
23     print('哇哦,次数用光啦!')
24     print(f'偷偷告诉你,你的lucky_day是{lucky_day}号。good luck')

运行截图

实验任务6

源代码

 1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
 2          '2049777002': ['音乐', '旅行'],
 3          '2049777003': ['马拉松', '健身', '游戏'],
 4          '2049777004': [],
 5          '2049777005': ['足球', '阅读'],
 6          '2049777006': ['发呆', '闲逛'],
 7          '2049777007': [],
 8          '2049777008': ['书法', '电影'],
 9          '2049777009': ['音乐', '阅读', '电影', '漫画'],
10          '2049777010': ['数学', '推理', '音乐', '旅行']
11         }
12 ls=[]
13 for value in datas.values():
14     for i in value:
15         ls.append(i)
16 h={}
17 for x in ls:
18     if x not in h:
19         h[x]=1
20     else:
21         h[x]+=1
22 lst1=list(hs.items())
23 lst2=[(value,key)for key,value in lst1]
24 for value,key in sorted(lst2,reverse=True):
25     print(f'{key}:{value}')

运行截图

实验任务7-1

源代码

 1 print('欢迎使用家用电器销售系统!')
 2 
 3 pro=[['0001','电视机','海尔',5999.00,20],
 4           ['0002','冰箱','西门子',6998.00,15],
 5           ['0003','洗衣机','小天鹅',1999.00,10],
 6           ['0004','空调','格力',3900.00,0],
 7           ['0005','热水器','格力',688.00,30],
 8           ['0006','笔记本','联想',5699.00,10],
 9           ['0007','微波炉','苏泊尔',480.00,33],
10           ['0008','投影仪','松下',1250.00,12],
11           ['0009','吸尘器','飞利浦',999.00,9]]
12 
13 products_cart=[]
14 
15 option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
16 while option in['1','2','3',]:
17     if option=='1':
18         print('产品和价格信息如下')
19         print('******************************************************************')
20         print('%-10s' % '编号', '%-10s' % '名称', '%-10s' % '品牌', '%-10s' % '价格', '%-10s' % '库存数量')
21         print('------------------------------------------------------------------')
22         for i in range(len(pro)):
23             print('%-10s'%pro[i][0],'%-10s'%pro[i][1],'%-10s'%pro[i][2],'%10.2f'%pro[i][3],'%10d'%pro[i][4])
24 
25         print('------------------------------------------------------------------')
26     elif option=='2':
27         pro_id=input('请输入您要购买的产品编号:')
28         while pro_id not in [item[0]for item in pro]:
29             pro_id=input('编号不存在,请重新输入您要购买的产品编号:')
30         count=int(input('请输入您要购买的产品数量:'))
31         while count > pro[int(pro_id)-1][4]:
32             count=input('数量超出库存。请重新输入您要购买的产品数量:')
33         if pro_id not in [item[0] for item in products_cart]:
34             products_cart.append([pro_id,count])
35         else:
36             for i in range(len(products_cart)):
37                 if products_cart[i][0]==pro_id:
38                     products_cart[i][1]+=count
39     else:
40         print('购物车信息如下:')
41         print('******************************************************************')
42         print('%-10s'%'编号','%-10s'%'购买数量')
43         print('------------------------------------------------------------------')
44         for i in range(len(products_cart)):
45             print('%-10s'%products_cart[i][0],'%6d'%products_cart[i][1])
46         print('------------------------------------------------------------------')
47     option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。')
48 
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][0]==products_cart[i][0]:
55                 pro_index=j
56                 break
57         price=pro[pro_index][3]
58         count=products_cart[i][1]
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 
70 print('谢谢您的光临,下次再见!')

运行截图

实验任务7-2

源代码

 1 print('欢迎使用家用电器销售系统!')
 2 
 3 pro=[['0001','电视机','海尔',5999.00,20],
 4           ['0002','冰箱','西门子',6998.00,15],
 5           ['0003','洗衣机','小天鹅',1999.00,10],
 6           ['0004','空调','格力',3900.00,0],
 7           ['0005','热水器','格力',688.00,30],
 8           ['0006','笔记本','联想',5699.00,10],
 9           ['0007','微波炉','苏泊尔',480.00,33],
10           ['0008','投影仪','松下',1250.00,12],
11           ['0009','吸尘器','飞利浦',999.00,9]]
12 
13 products_cart=[]
14 
15 option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
16 while option in['1','2','3',]:
17     if option=='1':
18         print('产品和价格信息如下')
19         print('******************************************************************')
20         print('{:<10} {:<10} {:<10} {:<10} {:<10}'.format('编号','名称','品牌','价格','库存数量'))
21         print('------------------------------------------------------------------')
22         for i in range(len(pro)):
23             print('{:<10}'.format(pro[i][0]), '{:<10}'.format(pro[i][1]), '{:<10}'.format(pro[i][2]),
24                   '{:<10.2f}'.format(pro[i][3]),
25                   '{:>10}'.format(pro[i][4]))
26 
27         print('------------------------------------------------------------------')
28     elif option=='2':
29         pro_id=input('请输入您要购买的产品编号:')
30         while pro_id not in [item[0]for item in pro]:
31             pro_id=input('编号不存在,请重新输入您要购买的产品编号:')
32         count=int(input('请输入您要购买的产品数量:'))
33         while count > pro[int(pro_id)-1][4]:
34             count=input('数量超出库存。请重新输入您要购买的产品数量:')
35         if pro_id not in [item[0] for item in products_cart]:
36             products_cart.append([pro_id,count])
37         else:
38             for i in range(len(products_cart)):
39                 if products_cart[i][0]==pro_id:
40                     products_cart[i][1]+=count
41     else:
42         print('购物车信息如下:')
43         print('*****************************************************************')
44         print('{:<10} {:<10} '.format('编号','购买数量'))
45         print('-----------------------------------------------------------------')
46         for i in range(len(products_cart)):
47             print('{:10s}'.format(products_cart[i][0]),'{:6d}'.format(products_cart[i][1]))
48         print('-----------------------------------------------------------------')
49     option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账。')
50 
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][0]==products_cart[i][0]:
57                 pro_index=j
58                 break
59         price=pro[pro_index][3]
60         count=products_cart[i][1]
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('谢谢您的光临,下次再见!')

运行截图

实验任务8-1

源代码

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

运行结果

 

标签:语句,count,pro,编程,数据类型,cart,products,print,id
From: https://www.cnblogs.com/zyj-/p/17332826.html

相关文章

  • 2022.4.22编程一小时打卡
    一、问题描述:请编写一个计数器Counter类,对其重载运算符“+”。二、解题思路:首先编写一个Counter类,然后,进行编写运算符“+”的重载,最后,进行代码的运行编译进行验证。三、代码实现:1#include<iostream>2#include<string>3usingnamespacestd;4classCounter5{......
  • 控制语句与组合数据类型应用编程
    importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whilelen(s2)......
  • 实验3 控制语句与组合数据类型应用编程
    实验任务1task1.py1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print(......
  • Qt5.12实战之图形编程初识
    演示效果: 1.绘制条件:1.绘图设备-> QPainter2.画笔->QPen --->字体(QFont)3.画刷->QBrush-->自己定义画刷(QPixmap)4.绘制事件->QPaintEvent绘图步骤:1.重写基类的虚函数 voidpaintEvent(QPaintEvent*event); 2.在虚函数 voidpaintEvent(QPaintEvent*event)的实现函......
  • java -- 网络编程
    软件结构C/S结构:全称为Client/Server结构,是指客户端和服务器结构。常见程序有QQ、迅雷等软件。B/S结构:全称为Browser/Server结构,是指浏览器和服务器结构。常见浏览器有谷歌、火狐等。网络通讯协议网络通信协议:通信协议是对计算机必须遵守的规则,只有遵守这些规则,计算机之间......
  • C语言发展历程、第一个C程序、数据类型、常量变量、字符串
    一、C语言的发展过程计算机是硬件,能识别电信号,电信号有两种,正电和负电,转化成数字信号1/0,计算机只能识别二进制指令,二进制语言是最早的低级语言。通过查表使用,只有科学家掌握。后来人们用一串二进制数表示一个功能,这个就叫助记符,如10100001-ADD,这就是汇编语言。后来人们想能不能用......
  • 基本数据类型
    基本数据类型数值类型整数类型byte占一个字节范围:-128-127(2的7次方)short占2个字节范围:-32768-32767(2的15次方)int占4个字节范围:-2147483648-2147483647(2的31次方)long占8个字节范围:-9223372036854775808-9223372036854775807(2的63次方)整数拓展进制二进制0b八进制0十进......
  • 基本数据类型
    基本数据类型数值类型整数类型byte占一个字节范围:-128-127(2的7次方)short占2个字节范围:-32768-32767(2的15次方)int占4个字节范围:-2147483648-2147483647(2的31次方)long占8个字节范围:-9223372036854775808-9223372036854775807(2的63次方)浮点类型float占4个字节double占8个......
  • 打卡 c语言趣味编程
     1.百钱百鸡#include <stdio.h>int main(){ int cock, hen, chicken; for (cock = 0; cock <= 20; cock++) { for (hen = 0; hen <= 33; hen++) { for (chicken = 0; chicken <= 100; chicken++) { if ((5 * cock + 3 * hen + chic......
  • 打卡2 c语言趣味编程
    3.抓逃犯#include <stdio.h>#include <math.h>int main(){ int a=0, b=0; //a:前两位,b:后两位 for (a = 0; a < 9; a++) { for (b = 0; b < 9; b++) { int c = a * 1000 + a * 100 + b * 10 + b; if (a != b &&sqrt(c)==(int)sqrt(......