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

task1

代码源码

 1 import random
 2 print('用列表存储随机整数:')
 3 list = [random.randint(0,100) for i in range(5)]
 4 print(list)
 5 
 6 print('\n用集合存储随机整数:')
 7 s1={random.randint(0,100) for i in range (5)}
 8 print(s1)
 9 
10 print('\n用集合存储随机整数:')
11 s2=set()
12 while  len(s2)<5:
13     s2.add(random.randint(0,100))
14 print(s2)

运行截图

 

答问题1:整数范围是(0,100),可以取到100

答问题2:利用 list(range(5)) 生成的有序序列范围是【0,4】的整数,不包括5

    利用 list(range(1,5)) 生成的有序序列范围是【1,4】,不包括5

答问题3:不一定是,因为取不到0值

答问题4:一定是

task2-1

代码源码

 1 list=[55,92,88,79,96]
 2 i=0
 3 while i <len(list):
 4     print(list[i],end=' ')
 5     i +=1
 6 print()
 7 
 8 for i in range(len(list)):
 9     print(list[i],end=' ')
10 print()
11 
12 for i in list:
13     print(i,end=' ')
14 print()

运行截图

 task2-2

代码源码

 1 book_info={'isbn':'978-7-5356-8297-0',
 2             '书名':'白鲸记',
 3             '作者':'克里斯多夫.夏布特',
 4             '译者':'高文婧',
 5             '出版社':'湖南美术出版社',
 6             '售价':82
 7             }
 8 
 9 for key,value in book_info.items():
10     print(f'{key}:{value}')
11 print()
12 
13 for item in book_info.items():
14     print(f'{item[0]}:{item[1]}')
15 print()
16 
17 for value in book_info.values():
18     print(value,end=' ')
19 print()
20 
21 for key in book_info.keys():
22     print(book_info[key],end=' ')

运行截图

task2-3

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]}')

 task3

 代码源码

 

 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.
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 
23 l=[text.lower().count(chr(i)) for i in range(97,97+26)]
24 d={}
25 for j in range(26):
26     d.update({chr(j+97):l[j]})
27 
28 l1=list(d.items())
29 l2=[(v,k) for k,v in l1]
30 for v,k in sorted(l2,reverse=True):
31     print(f'{k}:{v}')

 task4

 代码源码

 1 code_majors={8326:'地信类',
 2              8329:'计算机类',
 3              8330:'气科类',
 4              8336:'防灾工程',
 5              8345:'海洋科学',
 6              8382:'气象工程'}
 7 
 8 print(f'{"专业代号信息":-^50s}')
 9 for k,v in code_majors.items():
10     print(f'{k}:{v}')
11 
12 print(f'{"学生专业查询":-^50s}')
13 n=input('请输入学号:')
14 while n!='#':
15     if int(n[4:8]) in code_majors:
16       print(f'专业是:{code_majors.get(int(n[4:8]))}')
17     else:
18         print('不在这些专业中...')
19     n=input('请输入学号:')
20 print('查询结束...')

 

task5-1

代码源码

 1 import random
 2 print('猜猜2023年5月哪一天会是你的lucky day')
 3 a=random.randint(1,31)
 4 i=1
 5 n=int(input('你有3次机会,猜吧(1~31):  '))
 6 while i <=3:
 7     if i ==3:
 8         if n ==a:
 9             print('哇,猜中了')
10             break
11         elif int(n)<a:
12             print('猜早了,你的lucky day还没到呢')
13             print('哇哦,次数用光啦')
14             print('偷偷告诉你,5月你的lucky day是'+str(a)+'号.good luck')
15             break
16     elif i <3:
17         if n ==a:
18             print('哇,猜中了')
19             break
20         elif int(n) <a:
21             print('猜早了,你的lucky day还没到呢')
22             n=input('再猜(1~31):')
23             i=i+1
24             continue
25         elif int(n) >a:
26             print('猜晚了,你的lucky day已经过啦')
27             n=input('再猜(1~31):')
28             i=i+1
29             continue

运行截图

 

 

task6

代码源码

 1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
 2          '2049777002': ['音乐', '旅行'],
 3          '2049777003': ['马拉松', '健身', '游戏'],
 4          '2049777004': [],
 5          '2049777005': ['足球', '阅读'],
 6          '2049777006': ['发呆', '闲逛'],
 7          '2049777007': [],
 8          '2049777008': ['书法', '电影'],
 9          '2049777009': ['音乐', '阅读', '电影', '漫画'],
10          '2049777010': ['数学', '推理', '音乐', '旅行']
11          }
12 a=list(datas.values())
13 b=[str(x) for item in a for x in item]
14 c={}
15 for i in b :
16     c[i]=b.count(i)
17 d=list(c.items())
18 e=sorted(d,key=lambda x:x[1],reverse=True)
19 for m ,n in e:
20     print(f'{m}:{n}')

运行截图

 

task7-1

 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

 

 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('谢谢您的光临,下次再见!')

 task8-1

 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

 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('谢谢您的光临,下次再见!')

 

posted @ 2023-04-25 22:25  puppygaga  阅读(18)  评论(0)    收藏  举报