Python 完美購物車

2021-10-02 02:43:56 字數 2786 閱讀 8179

1.商家新增一些商品資訊

2.當使用者餘額足夠時,使用者可以購買,餘額自動減去對應的金額。當使用者餘額不足時,告知使用者購買失敗,並顯示使用者餘額

3.當使用者完成購物時,列印購物車中商品資訊

4.使用者可以輸入c或check列印商品資訊並繼續消費

5.使用者可以輸入q或quit退出程式,退出時,列印購物車中的商品資訊並顯示餘額

# 完美購物車程式練習

# 商品列表

product_list =[(

'iphone'

,5888),

('mac pro'

,8000),

('xiaomi'

,299),

('coffice',38

),('bao ma'

,400000),

('bike'

,1999),

('cloth'

,200)]

# 購物車

shop_car =

salary =

input

("input your salary:"

)# 判斷輸入是否是數字

if salary.isdigit():

salary =

int(salary)

else

: exit(

"invaild data type..."

)msg =

'welcome to night_du shopping mall'

.center(50,

'-')

print

(msg)

# 設定退出條件,為真退出程式

flag =

false

while flag is

nottrue

:print

('prodect 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:

# 買得起

# 放到購物車

salary -= p_item[1]

# 減錢

# \033[31;1m[%s]\033[0m 字型加顏色

print

("added [%s] into shop car,your current balance is \033[31;1m[%s]\033[0m"

%(p_item, salary)

)else

:print

('your balance is \033[31;1m[%s]\033[0m,cannot affords this...'

% salary)

else

:if user_choice ==

'q'or user_choice ==

'quit'

:# 判斷輸入的是否為q/quit

print

('purchased produce as below'

.center(40,

'-')

)for item in shop_car:

# 購物車裡面的商品

print

(item)

print

('end'

.center(40,

'-')

)print

("your balance is \033[31;1m[%s]\033[0m"

% salary)

# 餘額

print

("bye"

) flag =

true

# 退出程式

elif user_choice ==

'c'or user_choice ==

'check'

:# 檢視購物車中商品

print

('purchased produce as below'

.center(40,

'-')

)for item in shop_car:

print

(item)

print

('end'

.center(40,

'-')

)print

("your balance is \033[41;1m[%s]\033[0m"

% salary)

# \033[41;1m[%s]\033[0m 字型加顏色/背景顏色

購物車(註冊 登入 購物 購物車 結帳)

購物車 註冊 登入 購物 購物車 結帳 shopping car dict dict money 0 def input username pwd username input username pwd input pwd return username,pwd def goods get with...

python之購物車

下面是我們這個程式的框架 下面說一些 中比較難理解的 下面這個自己領會就好啦,本人不多做介紹 a if not a print a列表為空的 else print a列表不為空 下面主要介紹一下index這個函式,index在英語中是索引的意思,在這裡也一樣,它是用來看看某個值在列表中的索引是多少,...

Python實現購物車

encoding utf 8 定義列表商品 在這裡插入 片 product list iphone 100 mac 120 watch 218 bike 155 nike 299 adidas 266 shopping list 建立乙個空列表 salary input 使用者輸入工資 if sal...