Python基於數列實現購物車程序過程詳解

要求
數組

一、啓動程序後讓用戶輸入餘額,並打印商品列表
app

二、用戶經過輸入編號購買商品
ide

三、用戶選擇商品購買後,根據餘額判斷成功或者失敗,給出對應提示
學習

四、能夠隨時退出,退出後打印帳號餘額以及購買的商品列表spa

構思
code

一、首先,用戶餘額須要進行存儲,用戶購買的物品須要進行存儲在數組中
orm

二、用戶購買成功後,將購買的物品放入物品集合,並用總金額減去餘額
ci

三、若是失敗,給出失敗提示,並打印餘額
input

四、用戶選擇繼續後,不管成功失敗,均可以繼續購買string

代碼

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
# 用戶輸入工資
balance = int ( input ( "Please input balance:" ))
# 定義衣服的數組
clothes = [[ "pants" , 100 ],[ "T-shirt" , 50 ],[ "skirt" , 20 ]]
# 我的所得,包括金錢和獲取的物品
haveGoods = [balance,[]]
flag = True
while flag:
   # 打印衣服列表
   print ( "The clothes list is as follows" )
   print ( "______clothesList______" )
   i = 1 ;
   for c in clothes:
     print ( 'The number:' ,i, ":" ,c)
     i + = 1
 
   # 用戶輸入商品編號
   code = int ( input ( "Please choose the number:" ))
   # 判斷錢是否夠用
   if clothes[code - 1 ][ 1 ] < = haveGoods[ 0 ]:
     # 在本身的購物清單中加入已購物品
     haveGoods[ 1 ].append(clothes[code - 1 ])
     # 減去花費的金錢
     haveGoods[ 0 ] - = clothes[code - 1 ][ 1 ]
     print ( "You have successfully purchased!" )
     print ( "Your account balance is:" ,haveGoods[ 0 ])
   else :
     print ( "Your account balance is insufficient!" )
     print ( "Your account balance is:" ,haveGoods[ 0 ])
   judge = input ( "You can press any button to continue,or input 'n' to leave:" )
   if judge = = "n" :
     flag = False
print ( "Your account balance is:" ,haveGoods[ 0 ])
print ( "Your shopping list is as follows:" )
print ( "______clothesList______" )
for h in haveGoods[ 1 ]:
   print (h)

以上就是本文的所有內容,但願對你們的學習有所幫助,也但願你們多多支持腳本之家。

相關文章
相關標籤/搜索