国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python實現購物車功能的方法分析

Python實現購物車功能的方法分析

2020-12-17 00:22媛妹子 Python

這篇文章主要介紹了Python實現購物車功能的方法,結合實例形式分析了Python實現購物車功能的具體步驟、相關操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了Python實現購物車功能的方法。分享給大家供大家參考,具體如下:

1、程序的源代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
salary = input('input your salary:')
if salary.isdigit:
  salary = int(salary)
else:
  exit('salary is not digit!!')
welcome_msg = 'welcome to our shoping mall'
print(welcome_msg.center(50,'-'))
product_list = [
  ('Iphone',5888),
  ('Mac Air',8000),
  ('XiaoMi',19.9),
  ('coffee',30),
  ('Tesla',820000),
  ('Bike',700),
  ('Cloth',200)
]
shop_car = []
#推出標志位
exit_flag = 0
while exit_flag is not True:
  print('product list :'.center(50,'-'))
  for item in enumerate(product_list):
    index = item[0]   #獲得商品序號
    p_name = item[1][0#獲得商品名稱
    p_price= item[1][1#獲得商品價格
    print(index,p_name,p_price)
  user_choice = input('[q=quit,c=check] what do you want to buy?:')
  if user_choice.isdigit():
    user_choice = int(user_choice)
    if user_choice < len(product_list): #輸入的商品序號要在范圍之內
      p_item = product_list[user_choice] #選擇的商品
      if p_item[1] <= salary: #買得起
        shop_car.append(p_item) #放入購物車
        salary -= p_item[1] #減錢
        print('purchased products are:'.center(40, '-'))
        for item in shop_car:
          print(item)
        print('Your balance is [%s]' % salary)
      else:          #買不起
        print('Your balance is [%s],cannot buy the product!'%salary)
    else:
      print('we do not have the product,please input again!')
  elif user_choice == 'q' or user_choice == 'quit':
    print('purchased products are:'.center(40,'-'))
    for item in shop_car:
      print(item)
    print('END '.center(40,'-'))
    print('Your balance is [%s]'%salary)
    exit_flag =True
  elif user_choice == 'c' or user_choice == 'check':
    print('purchased products are:'.center(40, '-'))
    for item in shop_car:
      print(item)
    print('Your balance is %d' % salary)
    print('Check '.center(40, '-'))
  else:
    print('Incorrect input ,please input again!!')

2、程序運行結果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
input your salary:10000
-----------welcome to our shoping mall------------
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:0
added [[('Iphone', 5888)]] into shop car ,yourcurrent balence is 4112
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:1
Your balance is [4112],cannot buy the product!
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:c
shop car list are [[('Iphone', 5888)]],your balance is 4112
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:4
Your balance is [4112],cannot buy the product!
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:q
--------purchased products are:---------
('Iphone', 5888)
------------------END ------------------
Your balance is [4112]
Process finished with exit code 0

3、學習到的知識點

(1)print('product list :'.center(50,'-'))

?
1
2
>>> print('product list :'.center(50,'-'))
------------------product list :------------------

(2)for item in enumerate(product_list):

?
1
2
3
4
5
6
7
8
9
>>> for item in enumerate(product_list):
print(item)
(0, ('Iphone', 5888))
(1, ('Mac Air', 8000))
(2, ('XiaoMi', 19.9))
(3, ('coffee', 30))
(4, ('Tesla', 820000))
(5, ('Bike', 700))
(6, ('Cloth', 200))

enumerate函數返回一個生成器對象:(index,item)的元組。

(3)這里使用了列表+元組的形式存儲商品列表,而不是字典,因為字典是無序的,每次打印順序都不一樣,而且不能通過索引進行取值。

希望本文所述對大家Python程序設計有所幫助。

原文鏈接:http://blog.csdn.net/zora_lee/article/details/51635428

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩精品亚洲一区 | 欧美视频中文字幕 | 成人网免费看 | 欧美成人午夜视频 | 国产免费自拍 | 国产亚洲精品精品国产亚洲综合 | 成人激情在线 | 亚洲精品一区二区三区四区高清 | 国产亚洲精品久久久久久久 | 69国产精品成人96视频色 | 国产高清一区 | 中文字幕日韩在线视频 | 久久最新 | 欧美怡红院视频一区二区三区 | 国产午夜精品一区二区三区视频 | 久久精品无码一区二区三区 | 色天天天天色 | 欧美午夜精品久久久久久浪潮 | 久久的爱| 欧美成人精精品一区二区频 | 亚洲的天堂 | 国产成人一区二区三区 | 国产精品香蕉在线观看 | 欧美不卡| 一区二区三区久久 | 免费看黄在线网站 | 精品伊人久久 | 一区二区精品视频 | 国产精品久久久久久久久久 | 亚洲二区在线 | 91在线视频免费 | 成人在线一区二区 | 国产免费黄色 | 最近2019中文字幕大全视频10 | 亚洲一区中文字幕在线观看 | 爱爱免费视频 | 国产精品中文字幕在线 | 精品一区二区三区在线观看 | 看日韩毛片 | 国产情侣av自拍 | 黄色小视频在线观看 |