首页 > 其他分享 >实验三

实验三

时间:2023-04-25 22:01:26浏览次数:29  
标签:count 10 cart products print 实验 id

实验任务1

task1:

 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)

实验结果:

问题1:范围[1,100]闭区间,可以取到100

问题2:[0,4]闭区间,不包括5

             [1,4]闭区间,不包括5

问题3:不一定,集合没有顺序,元素随机排列

            不一定,集合没有顺序

 

实验任务2

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

实验结果:

 

task2_2:

 1 book_info = {'isbn': '978-7-5356-8297-0',
 2              '书名': 'naijingji',
 3              'zuozhe': 'krithdof.xiabute',
 4              'translater': 'gaowenjing',
 5              'chubanshe': 'hunanmeishu',
 6              'price': 82
 7             }
 8 for key, value in book_info.items():
 9     print(f'{key}: {value}')
10 print()
11 
12 for item in book_info.items():
13     print(f'{item[0]}: {item[1]}')
14 print()
15 
16 for value in book_info.values():
17     print(value, end=' ')
18 print()
19 
20 for key in book_info.keys():
21     print(book_info[key],end=' ')
22 print()

实验结果:

 

task2_3:

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

实验结果:

 

实验任务3

 task3:

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

实验结果:

 

 

实验任务4

task4:

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

实验结果:

 

 

实验任务5

task5:

 

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

 

实验结果:

 

 

 

实验任务6

task6:

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

实验结果;

 

 

实验任务7

task7:

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

实验结果:

 

task7_2:

 1 print('欢迎使用家用电器销售系统!')
 2 products = [
 3 ['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.50,33],
10 ['0008','投影仪','松下',1250.00,12],
11 ['0009','吸尘器','飞利浦',999.00,9]
12 ]
13 products_cart = []
14 option = input('请选择您的操作:1-查看商品;2-购物;3—查看购物车;其他-结账')
15 while option in['1', '2', '3']:
16     if option == '1':
17         print('产品和价格信息如下:')
18         print('{:*^48}'.format('*'))
19         print('{:10}'.format('编号'), '{:10}'.format('名称'), '{:10}'.format('品牌'), '{:10}'.format('价格'), '{:10}'.format('库存数量'))
20         print('{:-^50}'.format('-'))
21         for i in range(len(products)):
22             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]))
23         print('{:-^50}'.format('-'))
24     elif option == '2':
25         product_id = input('请输入您要购买的产品编号:')
26         while product_id not in [item[0] for item in products]:
27             product_id = input('编号不存在,请重新输入您要购买的产品编号:')
28 
29         count = int(input('请输入您要购买的产品数量:'))
30         while count > products[int(product_id)-1][4]:
31             count = int(input('数量超出库存,请重新输入您要购买的产品数量:'))
32         if product_id not in [item[0] for item in products_cart]:
33             products_cart.append([product_id,count])
34         else:
35             for i in range(len(products_cart)):
36                 if products_cart[i][0] == product_id:
37                     products_cart[i][1]+=count
38         for i in range(len(products)):
39             if products[i][0] == product_id:
40                 products[i][4] -= count
41     else:
42         print('购物车信息如下:')
43         print('{:*^48}'.format('*'))
44         print('{:10}'.format('编号'), '{:10}'.format('购买数量'))
45         print('{:-^50}'.format('-'))
46         for i in range(len(products_cart)):
47             print('{:10}'.format(products_cart[i][0]), '{:6}'.format(products_cart[i][1]))
48         print('{:-^50}'.format('-'))
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         product_index = 0
55         for j in range(len(products)):
56             if products[j][0] == products_cart[i][0]:
57                 product_index = j
58                 break
59         price = products[product_index][3]
60         count = products_cart[i][1]
61         amount += price*count
62 
63     if 5000 < amount <= 10000:
64         amount = amount * 0.95
65     elif 10000 < amount <= 20000:
66         amount = amount * 0.90
67     elif amount > 20000:
68         amount = amount*0.85
69     else:
70         amount = amount*1
71     print('购买成功,您需要支付%8.2f元' % amount)
72 print('谢谢您的光临,下次再见!')

实验结果:

 

实验任务8

task8_1:

 1 print('欢迎使用家用电器销售系统!')
 2 products=[
 3   {'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':580.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 products_cart = []
14 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
15 while option in ['1', '2', '3']:
16     if option == '1':
17         print('产品和价格信息如下:')
18         print(f'{"":*>48s}')
19         print(f'{"编号":10s}{"名称":10s}{"品牌":10s}{"价格":10s}{"库存数量":10s}')
20         print(f'{"":->50s}')
21         for i in range(len(products)):
22             print('{:10}'.format(products[i]['id']),'{:10}'.format(products[i]['name']),'{:10}'.format(products[i]['brand']),'{:10}'.format(products[i]['price']))
23         print(f'{"":->50s}')
24     elif option == '2':
25         product_id = input('请输入您要购买的商品编号:')
26         while product_id not in [item['id'] for item in products]:
27             product_id = input('编号不存在,请重新输入您要购买的产品编号:')
28         count = int(input('请输入您要购买的产品数量:'))
29         while count > products[int(product_id)-1]['count']:
30             count = int(input('数量超出库存,请重新输入您要购买的产品数量:'))
31         if product_id not in [item['id'] for item in products_cart]:
32             products_cart.append({'id':product_id,'count':count})
33         else:
34             for i in range(len(products_cart)):
35                 if products_cart[i].get('id') == product_id:
36                     products_cart[i]['count'] += count
37         for i in range(len(products)):
38             if products[i]['id'] == product_id:
39                 products[i]['count'] -= count
40     else:
41         print('购物车信息如下')
42         print(f'{"":*>48s}')
43         print(f'{"编号":10s}{"购买数量":10s}')
44         print(f'{"":->50s}')
45         for i in range(len(products_cart)):
46             print('{:10}'.format(products_cart[i]['id']),'{:6}'.format(products_cart[i]['count']))
47         print(f'{"":->50s}')
48     option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
49     if len(products_cart)>0:
50         amount=0
51         for i in range(len(products_cart)):
52             product_index = 0
53             for j in range(len(products)):
54                 if products[j]['id'] == products_cart[i]['id']:
55                     product_index = j
56                     break
57             price = products[product_index]['price']
58             count = products_cart[i]['count']
59             amount += price*count
60             if 5000 < amount <= 10000:
61                 amount = amount*0.95
62             elif 10000 < amount < 20000:
63                 amount = amount*0.90
64             elif amount > 20000:
65                 amount = amount*0.85
66             else:
67                 amount = amount*1
68             print('购买成功,您需要支付%8.2f'%amount)
69         print('谢谢您的光临,下次再见!')
70         

实验结果:

task8_2:

 1 print('欢迎使用家用电器销售系统!')
 2 products=[
 3   {'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':580.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 products_cart = []
14 option = input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
15 while option in ['1', '2', '3']:
16     if option == '1':
17         print('产品和价格信息如下:')
18         print(f'{"":*>48s}')
19         print(f'{"编号":10s}{"名称":10s}{"品牌":10s}{"价格":10s}{"库存数量":10s}')
20         print(f'{"":->50s}')
21         for i in range(len(products)):
22             print('{:10}'.format(products[i]['id']),'{:10}'.format(products[i]['name']),'{:10}'.format(products[i]['brand']),'{:10}'.format(products[i]['price']))
23         print(f'{"":->50s}')
24     elif option == '2':
25         product_id = input('请输入您要购买的商品编号:')
26         while product_id not in [item['id'] for item in products]:
27             product_id = input('编号不存在,请重新输入您要购买的产品编号:')
28         count = int(input('请输入您要购买的产品数量:'))
29         while count > products[int(product_id)-1]['count']:
30             count = int(input('数量超出库存,请重新输入您要购买的产品数量:'))
31         if product_id not in [item['id'] for item in products_cart]:
32             products_cart.append({'id':product_id,'count':count})
33         else:
34             for i in range(len(products_cart)):
35                 if products_cart[i].get('id') == product_id:
36                     products_cart[i]['count'] += count
37         for i in range(len(products)):
38             if products[i]['id'] == product_id:
39                 products[i]['count'] -= count
40     else:
41         print('购物车信息如下')
42         print(f'{"":*>48s}')
43         print(f'{"编号":10s}{"购买数量":10s}')
44         print(f'{"":->50s}')
45         for i in range(len(products_cart)):
46             print('{:10}'.format(products_cart[i]['id']),'{:6}'.format(products_cart[i]['count']))
47         print(f'{"":->50s}')
48     option=input('操作成功!请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
49     if len(products_cart)>0:
50         amount=0
51         for i in range(len(products_cart)):
52             product_index = 0
53             for j in range(len(products)):
54                 if products[j]['id'] == products_cart[i]['id']:
55                     product_index = j
56                     break
57             price = products[product_index]['price']
58             count = products_cart[i]['count']
59             amount += price*count
60             if 5000 < amount <= 10000:
61                 amount = amount*0.95
62             elif 10000 < amount < 20000:
63                 amount = amount*0.90
64             elif amount > 20000:
65                 amount = amount*0.85
66             else:
67                 amount = amount*1
68             print('购买成功,您需要支付%8.2f'%amount)
69         print('谢谢您的光临,下次再见!')

实验结果:

 

标签:count,10,cart,products,print,实验,id
From: https://www.cnblogs.com/gajs456asd5f4/p/17332771.html

相关文章

  • 实验3
    1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\n用集合存储随机......
  • 实验三
    1.实验任务1task1.py程序源代码:1importrandom2print('用列表存储随机整数:')3lst=[random.randint(0,100)foriinrange(5)]4print(lst)5print('\n用集合存储随机整数:')6s1={random.randint(0,100)foriinrange(5)}7print(s1)8print('\......
  • 实验3 控制语句与组合数据类型应用编程
    task1.pyimportrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whi......
  • python实验笔记1
    1.python如何在一行里面输入两个数呢如果直接这样子写会报错n=int(input())m=int(input())要按照下面的写法才可以实现n,m=map(int,input().split())2.python实现排列组合在itertools库中提供了两个函数permutations和combinations可以实现全排列和组......
  • 实验三
    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(......
  • 山东大学数据结构实验七
    卡片游戏tips:这个题还要参考,同学要加油啦~~要求创建队列类,使用数组描述的循环队列实现卡片游戏描述假设桌上有一叠扑克牌,依次编号为1-n(从上至下)。当至少还有两张的时候,可以进行操作:把第一张牌扔掉,然后把新的第一张(原先扔掉的牌下方的那张牌,即第二张牌)放到整叠牌的最后。......
  • 实验三
    #1实验内容:1#12importrandom3print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\n用集......
  • 山东大学数据结构实验六
    计算表达式tips:不要全文复制,会被查重哦注意因为精度问题,请使用double存数据。要求创建栈类,采用数组描述;计算数学表达式的值。输入数学表达式,输出表达式的计算结果。数学表达式由单个数字和运算符+、-、*、/、(、)构成,例如2+3*(4+5)-6/4。假定表达式输入格式合法。格式......
  • 山东大学数据结构实验一(2)
    题目描述现有一个有n个元素的序列\(a=[a_{1},a_{2},\cdots,a_{n}]\),定义其价值为\(\sum_{i=1}^{n}a_{i}\oplusi\)给出这样一个序列,求其所有排列的价值\(v_{i}\)的或\(v_{1}|v_{2}|\cdots|v_{n-1}|v_{n}\)其中\(|\)为位运算或操作,\(\oplus\)为位运算异......
  • 山东大学数据结构实验一(1)
    题目描述现有一个有\(n\)个元素的序列\(a=[a_1,a_2,\cdots,a_n]\),定义这个序列的价值为\(\sum_{i=1}^{n}i\timesa_i\)。空序列的价值为\(0\)。先给你一个长度为\(n\)的序列\(a\),求\(a\)中所有子集价值的异或和,要求子集中元素的相对位置保持不变。异或和:位运算的一种。如果a......