<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      實驗三 控制語句與組合數據類型應用編程

      實驗任務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: random.randint(0,100) 生成的隨機整數范圍是? 范圍:0~99

          能否取到100? 取不到。

      問題2:利用 list(range(5)) 生成的有序序列范圍是? 0~4

          是否包括5? 不包括。

          利用 list(range(1,5)) 生成的有序序列范圍是? [0,1,2,3,4]

          是否包括5? 不包括。

      問題3:使用line8代碼生成的集合s1,len(s1)一定是5嗎?如果不是,請解釋原因。 是。

      問題4:使用line12-14生成的集合s2,len(s2)一定是5嗎?如果不是,請解釋原因。 是。

      實驗任務2

      task2:

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

      2.1運行結果:

       

      2.2源代碼:

       1 # 字典遍歷
       2 book_info = {'isbn': '978-7-5356-8297-0',
       3 '書名': '白鯨記',
       4 '作者': '克里斯多夫.夏布特',
       5 '譯者': '高文婧',
       6 '出版社': '湖南美術出版社',
       7 '售價': 82
       8 }
       9 # 遍歷key-value對:實現方式1
      10 for key, value in book_info.items():
      11     print(f'{key}:{value}')
      12 print()
      13 
      14 # 遍歷key-value對:實現方式2
      15 for item in book_info.items():
      16     print(f'{item[0]}:{item[1]}')
      17 print()
      18 
      19 # 遍歷值:實現方式1
      20 for value in book_info.values():
      21     print(value, end = ' ')
      22 print()
      23 
      24 # 遍歷值: 實現方式2
      25 for key in book_info.keys():
      26     print(book_info[key], end = ' ')

      2.2運行結果:

       

      2.3源代碼:

       1 book_infos = [{'書名': '昨日的世界', '作者': '斯蒂芬.茨威格'},
       2 {'書名': '局外人', '作者': '阿爾貝.加繆'},
       3 {'書名': '設計中的設計', '作者': '原研哉'},
       4 {'書名': '萬歷十五年', '作者': '黃仁宇'},
       5 {'書名': '刀鋒', '作者': '毛姆'}
       6 ]
       7 i = 0
       8 while i < 5:
       9     j = book_infos[i]
      10     b = str(j['書名'])
      11     j['書名'] = b.join('《》')
      12     print(str(i+1)+"."+j['書名']+','+j['作者'])
      13     i +=1

      2.3運行結果:

       

      實驗任務3

      task3:

      源代碼:

       1 s = '''
       2 The Zen of Python, by Tim Peters
       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 s1 = s.lower()
      24 s2 = (chr(i) for i in range(97,123))
      25 ab = {}
      26 for i in s2:
      27     ab.setdefault(i,0)
      28 
      29 for key in ab.keys():
      30     ab[key] = s1.count(key)
      31 list1 = list(item for item in ab.items())
      32 list2 = [(value,key) for key, value in list1]
      33 for value,key in sorted(list2,reverse = True):
      34     print(f'{key}:{value}')

       

      運行結果:

       

       

       

      實驗任務4:

      task4:

      源代碼:

       1 major = {8326:'地信類',
       2 8329:'計算機類',
       3 8330:'氣科類',
       4 8336:'防災工程',
       5 8345:'海洋科學',
       6 8382:'氣象工程'}
       7 s1 = '專業代號信息'
       8 s2 = '學生專業查詢'
       9 print(f'{s1:-^50}')
      10 for key, value in major.items():
      11     print(f'{key}:{value}')
      12 print(f'{s2:-^50}')
      13 for i in major.keys():
      14     id = input('請輸入學號:')
      15     if id == '#':
      16         print(f'查詢結束...')
      17         break
      18     m = int(id[4:8])
      19     if m in major.keys():
      20         m2 = major[m]
      21         print(f'專業是:{m2}')
      22         pass
      23     else:
      24         print(f'不在這些專業中...')
      25         pass

      運行結果:

       

      實驗任務5

      task5:

      源代碼:

       1 import random
       2 i = random.randint(1,31)
       3 print(f'猜猜五月哪一天是你的lucky day\U0001F609')
       4 day = eval(input('你有三次機會,猜吧(1~31):'))
       5 for j in range(2):
       6     if day == i:
       7         print('哇,猜中了\U0001F923')
       8         break
       9     elif day > i:
      10         print('猜晚啦,你的lucky day已經過了。')
      11         day = eval(input('再猜(1~31):'))
      12         pass
      13     elif day < i:
      14         print('猜早啦,你的lucky day還沒到呢。')
      15         day = eval(input('再猜(1~31):'))
      16         pass
      17 print('哇哦,你的次數用光啦。')
      18 print(f'偷偷告訴你,5月你的lucky day是{i}號。good luck\U0001F60A')

      運行結果:

       

       

      實驗任務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 ih = set()
      13 ih1 = list()
      14 for value in datas.values():
      15     for i in value:
      16         ih1.append(i)
      17         ih.add(i)
      18 
      19 ih2 = {}
      20 for j in ih:
      21     ih2.setdefault(j,0)
      22 
      23 for y in ih1:
      24     if y in ih2.keys():
      25         ih2[y] += 1
      26     else:
      27         pass
      28 
      29 list1 = list(ih2.items())
      30 list2 = [(value, key) for key, value in list1]
      31 for value, key in sorted(list2,reverse = True):
      32     print(f'{key}:{value}')

       運行結果:

       

      實驗任務7

      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.50, 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('*'*60)
      31         print('%-10s'%'編號','%-10s'%'名稱','%-10s'%'品牌',
      32         '%-10s'%'價格','%-10s'%'庫存數量')
      33         print('-'*60)
      34         for i in range(len(products)):
      35             print('%-10s'%products[i][0],'%-10s'%products[i][1],'%-10s'%products[i][2],
      36             '%-10s'%products[i][3],'%-10s'%products[i][4])
      37         print('-'*60)
      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 
      51         else:
      52             for i in range(len(products_cart)):
      53                 if products_cart[i][0] == product_id:
      54                     products_cart[i][1] += count
      55 
      56         # 更新商品列表
      57         for i in range(len(products)):
      58             if products[i][0] == product_id:
      59                 products[i][4] -= count
      60 
      61     else:
      62         print('購物車信息如下:')
      63         print('*'*30)
      64         print('%-10s'%'編號','%-10s'%'購買數量')
      65         print('-'*30)
      66         for i in range(len(products_cart)):
      67             print('%-10s'%products_cart[i][0],'%6d'%products_cart[i][1])
      68         print('-'*30)
      69     option = input('操作成功!請選擇您的操作:1-查看商品;2-購物;3-查看購物車;其他-結賬')
      70 
      71 # 計算金額
      72 if len(products_cart) > 0:
      73     amount = 0
      74     for i in range(len(products_cart)):
      75         product_index = 0
      76         for j in range(len(products)):
      77             if products[j][0] == products_cart[i][0]:
      78                 product_index = j
      79                 break
      80         price = products[product_index][3]
      81         count = products_cart[i][1]
      82         amount += price*count
      83 
      84     if 5000<amount <= 10000:
      85         amount = amount*0.95
      86     elif 10000<amount <= 20000:
      87         amount = amount*0.9
      88     elif amount>20000:
      89         amount = amount*0.85
      90     else:
      91         amount = amount*1
      92 
      93 print('購買成功,您需要支付%8.2f元'%amount)
      94 
      95 # 退出系統
      96 print('謝謝您的光臨,下次再見!')

      運行結果:

      task7-2:

      str.format:

      源代碼:

       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.50, 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('*'*60)
      31         print('{:<10s}'.format('編號'),'{:<10s}'.format('名稱'),'{:<10s}'.format('品牌'),
      32         '{:<10s}'.format('價格'),'{:<10s}'.format('庫存數量'))
      33         print('-'*60)
      34         for i in range(len(products)):
      35             print('{:<10s}'.format(products[i][0]),
      36             '{:<10s}'.format(products[i][1]),
      37             '{:<10s}'.format(products[i][2]),
      38             '{:<10.2f}'.format(products[i][3]),
      39             '{:<10d}'.format(products[i][4]))
      40         print('-'*60)
      41     elif option == '2':
      42         product_id = input('請輸入您要購買的產品編號:')
      43         while product_id not in [item[0] for item in products]:
      44             product_id = input('編號不存在,請重新輸入您要購買的產品編號:')
      45 
      46         count = int(input('請輸入您要購買的產品數量:'))
      47         while count >products[int(product_id)-1][4]:
      48             count = int(input('數量超出庫存數量,請重新輸入您要購買的產品數量:'))
      49 
      50         # 將所購買商品加入購物車
      51         if product_id not in [item[0] for item in products_cart]:
      52             products_cart.append([product_id,count])
      53 
      54         else:
      55             for i in range(len(products_cart)):
      56                 if products_cart[i][0] == product_id:
      57                     products_cart[i][1] += count
      58 
      59         # 更新商品列表
      60         for i in range(len(products)):
      61             if products[i][0] == product_id:
      62                 products[i][4] -= count
      63 
      64     else:
      65         print('購物車信息如下:')
      66         print('*'*30)
      67         print('{:<10s}'.format('編號'),'{:<10s}'.format('購買數量'))
      68         print('-'*30)
      69         for i in range(len(products_cart)):
      70             print('{:<10s}'.format(products_cart[i][0]),'{:6d}'.format(products_cart[i][1]))
      71         print('-'*30)
      72     option = input('操作成功!請選擇您的操作:1-查看商品;2-購物;3-查看購物車;其他-結賬')
      73 
      74 # 計算金額
      75 if len(products_cart) > 0:
      76     amount = 0
      77     for i in range(len(products_cart)):
      78         product_index = 0
      79         for j in range(len(products)):
      80             if products[j][0] == products_cart[i][0]:
      81                 product_index = j
      82                 break
      83         price = products[product_index][3]
      84         count = products_cart[i][1]
      85         amount += price*count
      86 
      87     if 5000<amount <= 10000:
      88         amount = amount*0.95
      89     elif 10000<amount <= 20000:
      90         amount = amount*0.9
      91     elif amount>20000:
      92         amount = amount*0.85
      93     else:
      94         amount = amount*1
      95 
      96 print('購買成功,您需要支付{:<8.2f}元'.format(amount))
      97 
      98 # 退出系統
      99 print('謝謝您的光臨,下次再見!')

      運行結果:

      task7-3:

      f-string:

      源代碼:

       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.50, 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('*'*60)
      31         a = ['編號','名稱','品牌','價格','庫存數量','購買數量']
      32         print(f'{a[0]:10s}{a[1]:10s}{a[2]:10s}{a[3]:10s}{a[4]:10s}')
      33         print('-'*60)
      34         for i in range(len(products)):
      35             print(f'{products[i][0]:10s}{products[i][1]:10s}{products[i][2]:10s}{products[i][3]:10.2f}{products[i][4]:10d}')
      36         print('-'*60)
      37     elif option == '2':
      38         product_id = input('請輸入您要購買的產品編號:')
      39         while product_id not in [item[0] for item in products]:
      40             product_id = input('編號不存在,請重新輸入您要購買的產品編號:')
      41 
      42         count = int(input('請輸入您要購買的產品數量:'))
      43         while count >products[int(product_id)-1][4]:
      44             count = int(input('數量超出庫存數量,請重新輸入您要購買的產品數量:'))
      45 
      46         # 將所購買商品加入購物車
      47         if product_id not in [item[0] for item in products_cart]:
      48             products_cart.append([product_id,count])
      49 
      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 
      60     else:
      61         print('購物車信息如下:')
      62         print('*'*30)
      63         print(f'{a[0]:10s}{a[5]:10s}')
      64         print('-'*30)
      65         for i in range(len(products_cart)):
      66             print(f'{products_cart[i][0]:10s}{products_cart[i][1]:6d}')
      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][0] == products_cart[i][0]:
      77                 product_index = j
      78                 break
      79         price = products[product_index][3]
      80         count = products_cart[i][1]
      81         amount += price*count
      82 
      83     if 5000<amount <= 10000:
      84         amount = amount*0.95
      85     elif 10000<amount <= 20000:
      86         amount = amount*0.9
      87     elif amount>20000:
      88         amount = amount*0.85
      89     else:
      90         amount = amount*1
      91 
      92 print(f'購買成功,您需要支付{amount:8.2f}元')
      93 
      94 # 退出系統
      95 print('謝謝您的光臨,下次再見!')

      運行結果:

       

      實驗任務8

      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.50, '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'%'品牌',
      32         '%-10s'%'價格','%-10s'%'庫存數量')
      33         print('-'*60)
      34         for i in range(len(products)):
      35             print('%-10s'%products[i]['id'],
      36             '%-10s'%products[i]['name'],
      37             '%-10s'%products[i]['brand'],
      38             '%-10s'%products[i]['price'],
      39             '%-10s'%products[i]['count'])
      40         print('-'*60)
      41     elif option == '2':
      42         product_id = input('請輸入您要購買的產品編號:')
      43         while product_id not in [item['id'] for item in products]:
      44             product_id = input('編號不存在,請重新輸入您要購買的產品編號:')
      45 
      46         count = int(input('請輸入您要購買的產品數量:'))
      47         while count >products[int(product_id)-1]['count']:
      48             count = int(input('數量超出庫存數量,請重新輸入您要購買的產品數量:'))
      49 
      50         # 將所購買商品加入購物車
      51         if product_id not in [item['id'] for item in products_cart]:
      52             products_cart.append({'id':product_id,'count':count})
      53 
      54         else:
      55             for i in range(len(products_cart)):
      56                 if products_cart[i].get('id') == product_id:
      57                     products_cart[i]['count'] += count
      58 
      59         # 更新商品列表
      60         for i in range(len(products)):
      61             if products[i]['id'] == product_id:
      62                 products[i]['count'] -= count
      63 
      64     else:
      65         print('購物車信息如下:')
      66         print('*'*30)
      67         print('%-10s'%'編號','%-10s'%'購買數量')
      68         print('-'*30)
      69         for i in range(len(products_cart)):
      70             print('%-10s'%products_cart[i]['id'],'%6d'%products_cart[i]['count'])
      71         print('-'*30)
      72     option = input('操作成功!請選擇您的操作:1-查看商品;2-購物;3-查看購物車;其他-結賬')
      73 
      74 # 計算金額
      75 if len(products_cart) > 0:
      76     amount = 0
      77     for i in range(len(products_cart)):
      78         product_index = 0
      79         for j in range(len(products)):
      80             if products[j]['id'] == products_cart[i]['id']:
      81                 product_index = j
      82                 break
      83         price = products[product_index]['price']
      84         count = products_cart[i]['count']
      85         amount += price*count
      86 
      87     if 5000<amount <= 10000:
      88         amount = amount*0.95
      89     elif 10000<amount <= 20000:
      90         amount = amount*0.9
      91     elif amount>20000:
      92         amount = amount*0.85
      93     else:
      94         amount = amount*1
      95 
      96 print('購買成功,您需要支付%8.2f元'%amount)
      97 
      98 # 退出系統
      99 print('謝謝您的光臨,下次再見!')

      運行結果:

      task8-2:

      str.format:

      源代碼:

       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.50, '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}'.format('編號'),'{:<10s}'.format('名稱'),'{:<10s}'.format('品牌'),
      32         '{:<10s}'.format('價格'),'{:<10s}'.format('庫存數量'))
      33         print('-'*60)
      34         for i in range(len(products)):
      35             print('{:<10s}'.format(products[i]['id']),
      36             '{:<10s}'.format(products[i]['name']),
      37             '{:<10s}'.format(products[i]['brand']),
      38             '{:<10.2f}'.format(products[i]['price']),
      39             '{:<10d}'.format(products[i]['count']))
      40         print('-'*60)
      41     elif option == '2':
      42         product_id = input('請輸入您要購買的產品編號:')
      43         while product_id not in [item['id'] for item in products]:
      44             product_id = input('編號不存在,請重新輸入您要購買的產品編號:')
      45 
      46         count = int(input('請輸入您要購買的產品數量:'))
      47         while count >products[int(product_id)-1]['count']:
      48             count = int(input('數量超出庫存數量,請重新輸入您要購買的產品數量:'))
      49 
      50         # 將所購買商品加入購物車
      51         if product_id not in [item['id'] for item in products_cart]:
      52             products_cart.append({'id':product_id,'count':count})
      53 
      54         else:
      55             for i in range(len(products_cart)):
      56                 if products_cart[i].get('id') == product_id:
      57                     products_cart[i]['count'] += count
      58 
      59         # 更新商品列表
      60         for i in range(len(products)):
      61             if products[i]['id'] == product_id:
      62                 products[i]['count'] -= count
      63 
      64     else:
      65         print('購物車信息如下:')
      66         print('*'*30)
      67         print('{:<10s}'.format('編號'),'{:<10s}'.format('購買數量'))
      68         print('-'*30)
      69         for i in range(len(products_cart)):
      70             print('{:<10s}'.format(products_cart[i]['id']),'{:6d}'.format(products_cart[i]['count']))
      71         print('-'*30)
      72     option = input('操作成功!請選擇您的操作:1-查看商品;2-購物;3-查看購物車;其他-結賬')
      73 
      74 # 計算金額
      75 if len(products_cart) > 0:
      76     amount = 0
      77     for i in range(len(products_cart)):
      78         product_index = 0
      79         for j in range(len(products)):
      80             if products[j]['id'] == products_cart[i]['id']:
      81                 product_index = j
      82                 break
      83         price = products[product_index]['price']
      84         count = products_cart[i]['count']
      85         amount += price*count
      86 
      87     if 5000<amount <= 10000:
      88         amount = amount*0.95
      89     elif 10000<amount <= 20000:
      90         amount = amount*0.9
      91     elif amount>20000:
      92         amount = amount*0.85
      93     else:
      94         amount = amount*1
      95 
      96 print('購買成功,您需要支付{:<8.2f}元'.format(amount))
      97 
      98 # 退出系統
      99 print('謝謝您的光臨,下次再見!'

      運行結果:

      task8-3:

      f-string:

      源代碼:

       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.50, '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         a = ['編號','名稱','品牌','價格','庫存數量','購買數量']
      32         print(f'{a[0]:10s}{a[1]:10s}{a[2]:10s}{a[3]:10s}{a[4]:10s}')
      33         print('-'*60)
      34         for i in range(len(products)):
      35             print(f'{products[i]["id"]:10s}{products[i]["name"]:10s}{products[i]["brand"]:10s}{products[i]["price"]:10.2f}{products[i]["count"]:10d}')
      36         print('-'*60)
      37     elif option == '2':
      38         product_id = input('請輸入您要購買的產品編號:')
      39         while product_id not in [item['id'] for item in products]:
      40             product_id = input('編號不存在,請重新輸入您要購買的產品編號:')
      41 
      42         count = int(input('請輸入您要購買的產品數量:'))
      43         while count >products[int(product_id)-1]['count']:
      44             count = int(input('數量超出庫存數量,請重新輸入您要購買的產品數量:'))
      45 
      46         # 將所購買商品加入購物車
      47         if product_id not in [item['id'] for item in products_cart]:
      48             products_cart.append({'id':product_id,'count':count})
      49 
      50         else:
      51             for i in range(len(products_cart)):
      52                 if products_cart[i].get('id') == product_id:
      53                     products_cart[i]['count'] += count
      54 
      55         # 更新商品列表
      56         for i in range(len(products)):
      57             if products[i]['id'] == product_id:
      58                 products[i]['count'] -= count
      59 
      60     else:
      61         print('購物車信息如下:')
      62         print('*'*30)
      63         print(f'{a[0]:10s}{a[5]:10s}')
      64         print('-'*30)
      65         for i in range(len(products_cart)):
      66             print(f'{products_cart[i]["id"]:10s}{products_cart[i]["count"]:6d}')
      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.9
      87     elif amount>20000:
      88         amount = amount*0.85
      89     else:
      90         amount = amount*1
      91 
      92 print(f'購買成功,您需要支付{amount:8.2f}元')
      93 
      94 # 退出系統
      95 print('謝謝您的光臨,下次再見!')

       

      運行結果:

       

       

       實驗總結:

      通過本次實驗你收獲的具體知識點、思考等的歸納和梳理

      回答:字典和集合的使用方法。

       

      通過本次實驗你的新發現、體會/感受;尚存的問題

      體會:多樣的表示方法使得代碼更加易懂和簡潔。

      問題:內置函數的使用還不夠靈活,應該多加練習。

      其它你愿意反饋、分享的內容

      無。

      posted on 2023-04-26 01:07  DTong  閱讀(37)  評論(0)    收藏  舉報

      導航

      主站蜘蛛池模板: 91福利一区福利二区| 亚洲欧美日韩综合在线丁香| 武鸣县| 日本一区二区三区专线| 国产视频最新| 50路熟女| 国产内射性高湖| 开心五月深深爱天天天操| 精品人妻无码一区二区三区| 亚洲av熟女国产一二三| 免费无码成人AV片在线| 日本乱子人伦在线视频| 亚洲精品一区二区三区小| 久久综合九色综合97婷婷| 狠狠色噜噜狠狠狠狠777米奇| 日韩一区二区大尺度在线| 日本视频一两二两三区| 国产精品内射在线免费看| 亚洲欧洲日韩精品在线| 深夜福利视频在线播放| 人妻在线中文字幕| 亚洲国产高清第一第二区| 在线看av一区二区三区 | 草草浮力地址线路①屁屁影院| av一本久道久久综合久久鬼色| 狠狠色噜噜狠狠狠狠777米奇| 洛浦县| 色国产视频| 亚洲国产综合自在线另类| 国产精品美女www爽爽爽视频 | 中文字幕乱妇无码AV在线| 国产精品一区二区不卡91| 亚洲 都市 无码 校园 激情| 欧美人妻在线一区二区| 亚洲av成人一区二区三区| 国产又色又爽又黄的免费软件| 亚洲精品一区久久久久一品av| www免费视频com| 免费A级毛片无码A∨蜜芽试看| 国产成人综合在线观看不卡| 四虎永久免费高清视频|