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

任务一

 1 import random
 2 
 3 print('用列表存储随机整数')
 4 lis = [random.randint(0,100) for i in range(5)]
 5 print(lis)
 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)
16 
17 #随机数范围是1~100, 可以取到100
18 #有序序列的范围是:0~4, 不包括5;有序数列的范围是:1~4, 不包括5
19 #不一定是5;因为集合会自动过滤重复的数字
20 #一定是5;因为是是用来条件判断语句

结果

 

任务二

task2_1

 1 lst = [55,92,88,79,96]
 2 
 3 i = 0
 4 while i < len(lst):
 5     print(lst[i], end = ' ')
 6     i += 1
 7 print()
 8 
 9 
10 for i in range(len(lst)):
11     print(lst[i], end = ' ')
12 print()
13 
14 
15 for i in lst:
16     print(i, end = ' ')
17 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(1, 6):
 8     print(f'{i}.', end = ' ')
 9     lst = []
10     for value in book_infos[i-1].values():
11         lst.append(value)
12     for x in range(2):
13         if x == 0:
14             print(f'《{lst[x]}》',end = ' ,')
15         else:
16             print(f'{lst[x]}')
17     print()

结果

 

任务三

 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 text1 = str(text).lower()
24 text2 = list(text1.split())
25 s = {}
26 for x in text2:
27     bk = list(','.join(x))
28     for i in bk:
29         if i not in s:
30             s.update({i:1})
31         else:
32             s[i] += 1
33 s1 = {}
34 for item in s.items():
35     if 'a' <= item[0] <= 'z':
36         s1.update({item[0]:item[1]})
37     else:
38         continue
39 import operator
40 s2 = sorted(s1.items(),key = operator.itemgetter(1),reverse = True)
41 for (v,k) in s2:
42     print(f'{v}:{k}')

结果

 

任务四

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

结果

 

任务五

 1 import random
 2 luck_day = random.randint(1,31)
 3 print('猜猜2023年5月哪一天会是你的luck day\U0001F973 ')
 4 n = int(input('你有三次机会,猜吧(1~31):'))
 5 for i in range(3):
 6     if n<1 or n>31:
 7         print('地球上没有这一天啦,你是外星人吧!\U0001F921')
 8     elif n > luck_day:
 9         print('猜晚了,你的luckday早过了\U0001F63F')
10     elif n < luck_day:
11         print('猜早了,你的luckday还没到呢\U0001F63C')
12     else:
13         print('猜中了\U0001F63A')
14         break
15     if i == 2:
16         print('哇哦,次数用完啦')
17         print(f'偷偷告诉你,5月你的lucky day是{luck_day}号\U0001F63D')
18         break
19     n = int(input('再猜(1~31)\U0001F63B'))

结果

 

任务六

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

结果

 

任务七

task7_1

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

结果

task7_2

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

结果

 

 

任务八

task8_1

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

结果

task8_2

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

结果

 

posted @ 2023-04-25 20:46  柠七拧巴  阅读(32)  评论(0)    收藏  举报