实验三

发布时间 2023-04-25 11:03:05作者: 我真呵呵

实验任务一

实验源码:

 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)
task1

实验截图:

 

实验任务二

实验源码:

 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 for i in range(len(lst)):
10     print(lst[i], end = ' ')
11 print()
12 
13 for i in lst:
14     print(i, end = ' ')
15 print()
tsak2_1

实验截图:

实验源码:

 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 = ' ')
tsak2_2

实验截图:

实验源码:

 1 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
 2               {'书名': '局外人', '作者': '阿尔贝.加缪'},
 3               {'书名': '设计中的设计', '作者': '原研哉'},
 4               {'书名': '万历十五年', '作者': '黄仁宇'},
 5               {'书名': '刀锋', '作者': '毛姆'}
 6               ]
 7 for i in range(5):
 8     print(f'{i + 1}.', end = '')
 9     d = book_infos[i]
10     x = d['书名']
11     y = d['作者']
12     print(f'《{x}》, {y}')
tsak2_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.
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 x = text.lower()
24 d = {}
25 for i in range(97,123):
26     d[x.count(chr(i))] = chr(i)
27 y = sorted(d, reverse = True)
28 for i in y:
29     print(f'{d[i]}:{i}')
task3

实验截图:

 

实验任务四

实验源码:

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

实验截图:

 

实验任务五

实验源码:

 1 import random
 2 day = random.randint(1,31)
 3 i = 0
 4 while i < 3:
 5     x = int(input())
 6     if x < 1 or x > 31:
 7         print('地球上没有这一天啦,你是外星人吗')
 8     elif x == day:
 9         print('哇,猜中了')
10         break
11     elif x > day:
12         print('猜晚了')
13     elif x < day:
14         print('猜早了')
15     i += 1
16 if i == 3:
17     print('哇哦,次数用光啦')
18     print(f'偷偷告诉你,5月你的lucky day是{day}号.good luck')
task_5

实验截图:

 

实验任务六

实验源码:

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

实验截图:

 

实验任务七

实验源码:

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

实验截图: