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

实验任务1

代码

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

结果

 实验任务2

代码

 1 lst = [55, 92, 88, 79, 96]
 2 i = 0
 3 while i < len(lst):
 4  print(lst[i], end = ' ')
 5  i += 1
 6 print()
 7 for i in range(len(lst)):
 8  print(lst[i], end = ' ')
 9 print()
10 for i in lst:
11  print(i , end = ' ')
12 print()

结果

 代码

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

结果

 代码

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

结果

 实验任务3

代码

 1 s='''The Zen of Python,by Tim Petres
 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 s=s.lower()
22 dic={}
23 for i in range(26):
24     dic.setdefault(chr(i+97),s.count(chr(i+97)))
25 list1=sorted(dic.items())
26 m=[]
27 for i in list1:
28  m.append(list(i))
29 for i in range(len(m)):
30     m[i][1],m[i][0]=m[i][0],m[i][1]
31 list2=sorted(m)[::-1]
32 for i in range(len(list2)):
33     print(list2[i][1],':',list2[i][0],'\n',end='')

结果

 实验任务4

代码

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

结果

 实验任务5

代码

 1 import random
 2 day=random.randint(1,31)
 3 print('猜猜2023年5月哪一天会是你的lucky day','\U0001F917')
 4 n=int(input('你有三次机会,猜吧:'))
 5 if n==day:
 6     print('哇,猜中了','\U0001F917')
 7 elif n not in [i for i in range(1,32)]:
 8     print('地球上没有这一天啦,你是外星人吗')
 9     for i in range(2):
10      n=int(input('再猜(1~31):'))
11      if n == day:
12          print('哇,猜中了', '\U0001F917')
13          break
14      elif n not in [i for i in range(1, 32)]:
15          print('地球上没有这一天啦,你是外星人吗')
16      elif n > day:
17          print('猜晚了,你的lucky day已经过了')
18      else:
19          print('猜早了,你的lucky day还没到呢')
20 elif n>day:
21     print('猜晚了,你的lucky day已经过了')
22     for i in range(2):
23      n=int(input('再猜(1~31):'))
24      if n == day:
25          print('哇,猜中了', '\U0001F917')
26          break
27      elif n not in [i for i in range(1, 32)]:
28          print('地球上没有这一天啦,你是外星人吗')
29      elif n > day:
30          print('猜晚了,你的lucky day已经过了')
31      else:
32          print('猜早了,你的lucky day还没到呢')
33 else:
34     print('猜早了,你的lucky day还没到呢')
35     for i in range(2):
36      n=int(input('再猜(1~31):'))
37      if n == day:
38          print('哇,猜中了', '\U0001F917')
39          break
40      elif n not in [i for i in range(1, 32)]:
41          print('地球上没有这一天啦,你是外星人吗')
42      elif n > day:
43          print('猜晚了,你的lucky day已经过了')
44      else:
45          print('猜早了,你的lucky day还没到呢')
46 
47 if n!=day:
48     print('哇哦,次数用光了')
49     print('偷偷告诉你,5月你的lucky day是',day,'')

结果

 实验任务6

代码

 1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
 2 '2049777002': ['音乐', '旅行'],
 3 '2049777003': ['马拉松', '健身', '游戏'],
 4 '2049777004': [],
 5 '2049777005': ['足球', '阅读'],
 6 '2049777006': ['发呆', '闲逛'],
 7 '2049777007': [],
 8 '2049777008': ['书法', '电影'],
 9 '2049777009': ['音乐', '阅读', '电影', '漫画'],
10 '2049777010': ['数学', '推理', '音乐', '旅行']
11 }
12 list1=[]
13 for i in datas:
14     for j in datas[i]:
15      list1.append(j)
16 set1=set(list1)
17 dic={}
18 for i in set1:
19  dic.setdefault(i,list1.count(i))
20 list2=list(dic.items())
21 for i in range(len(list2)):
22     list2[i]=list(list2[i])
23     list2[i][0],list2[i][1]=list2[i][1],list2[i][0]
24 list2=(sorted(list2))[::-1]
25 for i in range(len(list2)):
26  print(list2[i][1],':',list2[i][0])

结果

 实验任务7

代码

 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 option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
22 while option in ['1','2','3']:
23   if option=='1':
24       #产品信息列表
25       print('产品和价格信息如下:')
26       print('********************************************')
27       print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
28       print('--------------------------------------------')
29       for i in range(len(products)):
30         print ( '%-10s'%products[i][0],'%-10s'%products[i][1],'%-10s'%products[i][2],'%10.2f'%products[i][3],'%10d'%products[i][4])
31       print('--------------------------------------------')
32   elif option=='2':
33    product_id= input('请输入您要购买的产品编号:')
34    while product_id not in[item[0] for item in products]:
35      product_id=input('编号不存在,请重新输入您要购买的产品编号:')
36 
37    count=int(input('请输入您要购买的产品数量:'))
38    while count> products[int( product_id)-1][4]:
39      count=int(input('数量超出库存,请重新输入您要购买的产品数量:'))
40 
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 products_cart[i][0]==product_id:
47          products_cart[i][1]+=count
48 
49 #更新商品列表
50    for i in range(len(products)):
51     if products[i][0]==product_id:
52      products[i][4]-=count
53   else:
54    print('购物车信息如下:')
55    print('*******************************************')
56    print('%-10s'%'编号','%-10s'%'购买数量')
57    print('-------------------------------------------')
58    for i in range(len(products_cart)):
59     print('%-10s'%products_cart[i][0],'%-10s'%products_cart[i][1])
60    print('-------------------------------------------')
61   option=input('操作成功!请选择您的操作:1—查看物品;2-购物;3-查看购物车;其它-结账')
62 #计算金额
63 if len( products_cart)>0:
64  amount=0
65  for i in range( len( products_cart)):
66   product_index=0
67   for j in range(len(products)):
68    if products[j][0]== products_cart[i][0]:
69       product_index=j
70       break
71   price= products[ product_index][3]
72   count= products_cart[i][1]
73   amount+=price*count
74  if 5000<amount<=10000:
75   amount=amount*0.95
76  elif 10000<amount <=20000:
77   amount=amount*0.90
78  elif amount>20000:
79   amount=amount*0.85
80  else:
81   amount=amount*1
82  print('购买成功,您需要支付%8.2f元'%amount)
83 #退出系统
84 print('谢谢您的光临,下次再见!')

结果

 代码

 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 option=input('请选择您的操作:1-查看商品;2-购物;3-查看购物车;其他-结账')
22 while option in ['1','2','3']:
23   if option=='1':
24       #产品信息列表
25       print('产品和价格信息如下:')
26       print('********************************************')
27       print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
28       print('--------------------------------------------')
29       for i in range(len(products)):
30         print ( '%-10s'%products[i][0],'%-10s'%products[i][1],'%-10s'%products[i][2],'%10.2f'%products[i][3],'%10d'%products[i][4])
31       print('--------------------------------------------')
32   elif option=='2':
33    product_id= input('请输入您要购买的产品编号:')
34    while product_id not in[item[0] for item in products]:
35      product_id=input('编号不存在,请重新输入您要购买的产品编号:')
36 
37    count=int(input('请输入您要购买的产品数量:'))
38    while count> products[int( product_id)-1][4]:
39      count=int(input('数量超出库存,请重新输入您要购买的产品数量:'))
40 
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 products_cart[i][0]==product_id:
47          products_cart[i][1]+=count
48 
49 #更新商品列表
50    for i in range(len(products)):
51     if products[i][0]==product_id:
52      products[i][4]-=count
53   else:
54    print('购物车信息如下:')
55    print('*******************************************')
56    print('%-10s'%'编号','%-10s'%'购买数量')
57    print('-------------------------------------------')
58    for i in range(len(products_cart)):
59     print('%-10s'%products_cart[i][0],'%-10s'%products_cart[i][1])
60    print('-------------------------------------------')
61   option=input('操作成功!请选择您的操作:1—查看物品;2-购物;3-查看购物车;其它-结账')
62 #计算金额
63 if len( products_cart)>0:
64  amount=0
65  for i in range( len( products_cart)):
66   product_index=0
67   for j in range(len(products)):
68    if products[j][0]== products_cart[i][0]:
69       product_index=j
70       break
71   price= products[ product_index][3]
72   count= products_cart[i][1]
73   amount+=price*count
74  if 5000<amount<=10000:
75   amount=amount*0.95
76  elif 10000<amount <=20000:
77   amount=amount*0.90
78  elif amount>20000:
79   amount=amount*0.85
80  else:
81   amount=amount*1
82  print('购买成功,您需要支付:','{:.2f}'.format(amount),'')
83 #退出系统
84 print('谢谢您的光临,下次再见!')

结果

 实验任务8

代码

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

 

结果

 代码

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

 

结果

 

posted @ 2023-04-20 00:43  胡梦欢  阅读(19)  评论(0编辑  收藏  举报