首页 > 其他分享 >实验三

实验三

时间:2023-04-25 10:56:47浏览次数:21  
标签:count product cart products print 实验 id

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.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。

 

 

task2_1

源代码:

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

运行截图:

 

 

task2_2

源代码:

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

运行截图:

 

task2_3

源代码:

1 book_infos = [{'书名':'昨日的世界','作者':'斯蒂芬.茨威格'},{'书名':'局外人','作者':'阿尔贝.加缪'},{'书名':'设计中的设计','作者':'原研哉'},{'书名':'万历十五年','作者':'黄仁宇'},{'书名':'刀锋','作者':'毛姆'}]
2 bookname = []
3 author = []
4 for i in range (len(book_infos)):
5     bookname.append(book_infos[i]['书名'])
6     author.append(book_infos[i]['作者'])
7 for i in range (0,5):
8     print(f'{i+1}.《{bookname[i]}》,{author[i]}')

运行截图:

 

 

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 list1 = list(text.lower().split())
24 d1 = dict.fromkeys('abcdefghijklmnopqrstuvwxyz',0)
25 for i in range (len(list1)):
26     for j in list1[i]:
27         if ord(j) in range(97,97+26):
28             d1[j] += 1
29         else:
30             continue
31 
32 d2 = sorted([(x[1],x[0]) for x in d1.items()],reverse = True)
33 d3 = [(x[1],x[0]) for x in d2]
34 for i in range (len(d3)):
35     print(f'{d3[i][0]}:{d3[i][1]}')

运行截图:

 

task4

源代码:

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

 

运行截图:

 

 

task5

源代码:

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

 

运行截图:

 

 

task6

源代码:

 1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
 2          '2049777002': ['音乐', '旅行'],
 3          '2049777003': ['马拉松', '健身', '游戏'],
 4          '2049777004': [],
 5          '2049777005': ['足球', '阅读'],
 6          '2049777006': ['发呆', '闲逛'],
 7          '2049777007': [],
 8          '2049777008': ['书法', '电影'],
 9          '2049777009': ['音乐', '阅读', '电影', '漫画'],
10          '2049777010': ['数学', '推理', '音乐', '旅行']
11          }
12 d1 = [(x[1],x[0]) for x in datas.items()]
13 l=[]
14 for i in range(len(list(datas.values()))):
15     for j in range(len(list(datas.values())[i])):
16         if list(datas.values())[i][j] not in l:
17             l.append(list(datas.values())[i][j])
18         else:
19             continue
20 print(l)
21 
22 types_count = dict.fromkeys(l,0)
23 for i in range(len(d1)):
24     for j in d1[i][0]:
25         if j != '':
26             types_count[j] += 1
27         else:
28             continue
29 
30 d2 = sorted([(x[1],x[0]) for x in types_count.items()],reverse = True)
31 d3 = [(x[1],x[0]) for x in d2]
32 for i in range (len(d3)):
33     print(f'{d3[i][0]}:{d3[i][1]}')

运行截图:

 

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('{:10}'.format('编号'), '{:10}'.format('名称'), '{:10}'.format('品牌'), '{:10}'.format('价格'), '{:10}'.format('库存数量'))
32         print('--------------------------------------------------------------')
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('--------------------------------------------------------------')
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('{:10}'.format('编号'), '{:10}'.format('购买数量'))
61         print('------------------------------')
62         for i in range(len(products_cart)):
63             print('{:10}'.format(products_cart[i][0]), '{:6}'.format(products_cart[i][1]))
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('购买成功,您需要支付%8.2f元'%amount)
89 
90 #退出系统
91 print('谢谢您的光临,下次再见!')

运行截图:

 

 

task8_1

源代码:

运行截图:

 

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('{:10}'.format(products[i]['id']),'{:10}'.format(products[i]['name']),'{:10}'.format(products[i]['brand']),'{:10}'.format(products[i]['price']))
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'{"编号":10s}{"购买数量":10s}')
61         print('------------------------------')
62         for i in range(len(products_cart)):
63             print('{:10}'.format(products_cart[i]['id']),'{:6}'.format(products_cart[i]['count']))
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:.2f}元')
89 
90 #退出系统
91 print('谢谢您的光临,下次再见!')

运行截图:

 

标签:count,product,cart,products,print,实验,id
From: https://www.cnblogs.com/ljxljx317/p/17332587.html

相关文章

  • 实验3
    实验3.1: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()while......
  • C语言实验报告范例
    实验报告四一,实验类型:设计型二,实验室:三,指导老师:四,日期:五,实验名称:if分支语句的嵌套六,实验目的:1,学习if嵌套结构,能够用C语言编程解决日常生活的实例2,明确if语句在实现分支结构控制语句方面的特点和优势3,熟练掌握关系运算符、关系表达式、逻辑运算符和逻辑表达式、......
  • 实验一 进程的调度
    对一些函数和知识的解释getpid():返回当前进程的PID(进程编号)pid_tC语言重新定义的数据类型(其实就是int,换了一个名字)用来存储进程的PID。pid_t定义在sys/types.h头文件中fork()函数:用于创建一个新进程作为当前进行的子进程。如果成功创建⼦进程,对于⽗⼦进程fork会返回......
  • 逆向-第三次实验-注册码分析
    本次实验考虑使用到了ida在使用ida时,用ida32而非64,否则无法查看伪代码。打开后看到dialog,即对话框元素函数,很有可能是我们要考虑的地方,点击后F5得到伪代码。 代码INT_PTR__stdcallDialogFunc(HWNDhWnd,UINTa2,WPARAMa3,LPARAMa4){HICONIconA;//eaxUINT......
  • 实验3 控制语句与组合数据类型应用编程
    实验任务一源代码1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\n用......
  • 实验3 控制语句与组合数据类型应用编程
    task1问题1:可以取到100问题2:范围:0、1、2、3、4,不能取到5;范围:1、2、3、4,不能取到5问题3:一定是5问题4:一定是5task2-3book_infos=[{'书名':'昨日的世界','作者':'斯蒂芬.茨威格'},{'书名':'局外人','作者':'阿尔贝.加缪'......
  • 实验3 控制语句与组合数据类型应用编程
    task1源代码1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('......
  • Java实验七
    1packageJavashiyan7a;2publicclassBikeimplementsVehicle{3@Override4publicvoidstart(){5System.out.println("Bikestart");6}78@Override9publicvoidstop(){10System.out.println......
  • 实验三
    任务一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()while......
  • 实验3
    实验3实验任务1task1.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()w......