实验任务1

task1

程序源代码

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

 

运行截图

 

 实验结论

1.random.randint(0,100)生成随机数整数的范围是[0,100],可以取到100。

2.list(range(5))生成的是[0, 1, 2, 3, 4],不包括5。

  list(range(1,5))生成的有序序列范围是[1, 2, 3, 4],不包括5.

3.不一定,随机数有可能重复,但只会出现一次。

4.一定是5。

 

 

 

实验任务2

task2_1

程序源代码

 1 #task2_1.py
 2 print('task2_1.py')
 3 
 4 #列表遍历
 5 lst = [55,92,88,79,96]
 6 
 7 #遍历方式1:使用while + 索引
 8 i = 0
 9 while i < len(lst):
10     print(lst[i], end = ' ')
11     i += 1
12 
13 print()
14 
15 
16 #遍历方式2:使用for + 索引
17 for i in range(len(lst)):
18     print(lst[i], end = ' ')
19 print()
20 
21 #遍历方式3:使用for... in
22 for i in lst:
23     print(i, end = " ")
24 print()

 

运行截图

 

 

task2_2

程序源代码

 1 #task2_2.py
 2 print('task2_2.py')
 3 
 4 #字典遍历
 5 book_info = {'isbn': '978-7-5356-8297-0',
 6              '书名': '白鲸记',
 7              '作者': '克里斯多夫.夏布特',
 8              '译者': '高文婧',
 9              '出版社': '湖南美术出版社',
10              '售价': 82
11              }
12 
13 #遍历key-value对:实现方式1
14 for key,value in book_info.items():
15     print(f'{key}:{value}')
16 print()
17 
18 #遍历key-value对:实现方式2
19 for item in book_info.items():
20     print(f'{item[0]}:{item[1]}')
21 print()
22 
23 #遍历值:实现方式1
24 for value in book_info.values():
25     print(value, end  = ' ')
26 print()
27 
28 #遍历值:实现方式2
29 for key in book_info.keys():
30     print(book_info[key], end = ' ')

 

运行截图

 

task2_3

程序员代码

 1 #task2_3.py
 2 print('task2_3.py')
 3 
 4 book_infos = [{'书名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
 5               {'书名': '局外人', '作者': '阿尔贝.加缪'},
 6               {'书名': '设计中的设计', '作者': '原研哉'},
 7               {'书名': '万历十五年', '作者': '黄仁宇'},
 8               {'书名': '刀锋', '作者': '毛姆'}
 9              ]
10 
11 lst = []
12 i = 0
13 while i < len(book_infos):
14     t = dict(book_infos[i])
15     for key in t.keys():
16         lst.append(t[key])
17     i += 1
18 x = 0
19 while x < 5:
20     print(f'{x + 1}. 《{lst[2*x]}》, {lst[2*x + 1]}')
21     x += 1

 

运行截图

 

 

 

实验任务3

task3

程序源代码

 1 #task3.py
 2 print('task3.py')
 3 
 4 text_original = '''The Zen of Python, by Tim Peters
 5 
 6 Beautiful is better than ugly.
 7 Explicit is better than implicit.
 8 Simple is better than complex.
 9 Complex is better than complicated.
10 Flat is better than nested.
11 Sparse is better than dense.
12 Readability counts.
13 Special cases aren't special enough to break the rules.
14 Although practicality beats purity.
15 Errors should never pass silently.
16 Unless explicitly silenced.
17 In the face of ambiguity, refuse the temptation to guess.
18 There should be one-- and preferably only one --obvious way to do it.
19 Although that way may not be obvious at first unless you're Dutch.
20 Now is better than never.
21 Although never is often better than *right* now.
22 If the implementation is hard to explain, it's a bad idea.
23 If the implementation is easy to explain, it may be a good idea.
24 Namespaces are one honking great idea -- let's do more of those!'''
25 text = text_original.lower()
26 d = {}
27 for i in range(97,97+26):
28     t = dict.fromkeys(chr(i),text.count(chr(i)))
29     d.update(t)
30 d1 = sorted(d.items(), key = lambda x:x[1], reverse = True)
31 for i in d1:
32     print(f'{i[0]}:{i[1]}')

运行截图

 

 

 

实验任务4

task4

程序源代码

 1 #task4.py
 2 print('task4.py')
 3 
 4 code_majors = [{'8326': '地信类'},
 5                {'8329': '计算机类'},
 6                {'8330': '气科类'},
 7                {'8336': '防灾工程'},
 8                {'8345': '海洋科学'},
 9                {'8382': '气象工程'}]
10 #遍历输出专业代号与专业名称
11 print(f"{'专业代号信息':-^50}")
12 for i in code_majors:
13     lst = list(i.items())
14     print(f'{lst[0][0]}: {lst[0][1]}')
15 
16 print(f"{'学生专业查询':-^50}")
17 code_majors_dict = dict()  #将code_majors转为一个包含多个键值对的字典
18 lst4 = []
19 for i in code_majors:
20     t = list(i.items())
21     code_majors_dict.update(i)
22     lst4.append(t[0][0])  #得到专业代码列表
23 
24 stu_num = input('请输入学号:')
25 while stu_num != '#':
26     lst2 = list(stu_num)
27     lst3 = lst2[4:8]
28     entered_code = ''.join(lst3)
29 
30     if entered_code in lst4:
31         print(f'专业是:{code_majors_dict[entered_code]}')
32     else:
33         print('不在这些专业中...')
34     stu_num = input('请输入学号:')
35 print('查询结束')

运行截图

 

 

 

实验任务5

task5

程序源代码

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

 

运行截图

 

 

 

实验任务6

task6

程序源代码

 1 #task6.py
 2 print('task6.py')
 3 
 4 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
 5          '2049777002': ['音乐', '旅行'],
 6          '2049777003': ['马拉松', '健身', '游戏'],
 7          '2049777004': [],
 8          '2049777005': ['足球', '阅读'],
 9          '2049777006': ['发呆', '闲逛'],
10          '2049777007': [],
11          '2049777008': ['书法', '电影'],
12          '2049777009': ['音乐', '阅读', '电影', '漫画'],
13          '2049777010': ['数学', '推理', '音乐', '旅行']
14         }
15 lst1 = list(datas.items())
16 lst2 = []
17 i = 0
18 while i < len(lst1):
19     lst2.append(lst1[i][1])
20     i += 1
21 lst3 = []
22 for a in range(0,len(lst2)):
23     for b in range(0,len(lst2[a])):
24         lst3.append(lst2[a][b])
25 lst4 = list(set(lst3))
26 d = {}
27 for i in range(len(lst4)):
28     t = {lst4[i]:lst3.count(lst4[i])}
29     d.update(t)
30 d1 = sorted(d.items(), key = lambda x:x[1], reverse = True)
31 for i in d1:
32     print(f'{i[0]}:{i[1]}')

 

运行截图

 

 

 

实验任务7

task7_1

程序源代码

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

 

运行截图

 

 

task7_2

程序源代码

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

 

运行截图

 

 

 

实验任务8

task8_1

程序源代码

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

 

运行截图