專案分享 模擬購物車

2022-06-20 23:24:19 字數 3513 閱讀 8368

使用者先給自己的賬戶充錢:比如先充3000元。

有如下的乙個格式:

goods = [

, , ,,

]

頁面顯示序號 + 商品名稱 + 商品**,如:1 電腦 1999

使用者輸入選擇的商品序號,然後列印商品名稱及商品**, 並將此商品,新增到購物車(自己定義購物車),使用者還可繼續新增商品。

如果使用者輸入的商品序號有誤,則提示輸入有誤,並重新輸入。

使用者輸入n為購物車結算,依次顯示使用者購物車裡面的商品,數量及單價,若充值的錢數不足,則讓使用者刪除某商品,直至可以購買,若充值的錢數充足,則可以直接購買。

使用者輸入q或者q退出程式。

退出程式之後,依次顯示使用者購買的商品,數量,單價,以及此次共消費多少錢,賬戶餘額多少,並將購買資訊顯示。

goods = [,,,

, ]shopping_trolley = # 購物車

bill = # 結算賬單

print("歡迎來到**".center(18, "*"))

while true:

money = input("請充值金額:")

if money.isdigit() and int(money) > 0: # 必須是大於0的整數

print(f"本次您充值的金額為:元")

print("商品資訊".center(18, "*"))

for i, el in enumerate(goods, 1): # 列舉解包出序號

value_lst = list(el.values())

print(f"序號: 商品: **:")

while true:

number = input("請輸入你想購買的商品序號(按n結算,按q退出):")

if number.upper() != "q":

if number.isdigit() and 0 < int(number) < len(goods) + 1:

number = int(number)

commodity = (number, goods[number - 1]["name"], goods[number - 1]["price"]) # 選中的物品打包

print(f"商品: **:") # 格式化輸出購買的商品

elif number.upper() == "n": # 結算

if shopping_trolley == :

print("購物車中無商品,請重新選擇商品!")

else:

print("\n"+"您已選擇以下商品".center(18, "*"))

price_sum = 0 # 總**初始化

remove_repeat = set(shopping_trolley) # 去重購物車

for el in remove_repeat:

time = shopping_trolley.count(el) # 計算重複商品出現的次數

num, name, price = el # 解包元組

print(f"商品: **: 數量:")

price_sum = price_sum + price * time # 計算**

while true:

if price_sum > int(money):

print("\n您充值的金額不足,請選擇刪除購物車中的商品!")

print("\n"+"購物車".center(18, "*"))

choose_num =

remove_repeat = set(shopping_trolley) # 重複**,目的再次展示一邊購物車的東西

for el in remove_repeat:

time = shopping_trolley.count(el)

num, name, price = el

print(f"序號: 商品: **: 數量:") # 再次展示一邊購物車的東西

number = input("請輸入你想刪除的商品序號:")

if number.isdigit() and int(number) in choose_num: # 判斷輸入的序號在不在購物車中

if len(shopping_trolley) > 1:

for i in range(len(shopping_trolley)):

if shopping_trolley[i][0] == int(number):

price_sum = price_sum - shopping_trolley[i][2]

shopping_trolley.pop(i) # 刪除

break # 每次只刪乙個,所以不存在迴圈列表刪不乾淨的問題

else: # 此時購物車中只剩一件商品

shopping_trolley.clear() # 直接清空就好了

print("購物車中無商品!")

price_sum = 0 # 商品總價清零

break

else:

print("您輸入的序號有誤,請重新輸入!")

else:

choose = input("您的金額充足,確認是否購買(是/否)?")

if choose == "是":

print("購買成功!")

for el in shopping_trolley:

shopping_trolley.clear() # 清空購物車

price_sum = 0 # 商品總價歸零

break

elif choose == "否":

shopping_trolley.clear()

print("已清空購物車!")

break

else:

print("您的輸入有誤,請重新輸入!")

else:

print("您輸入的序號有誤,請重新輸入!")

else:

print("結算清單".center(18, "*"))

print("本次您購買的商品如下:")

shopping_trolley.clear()

price_sum = 0

remove_repeat = set(bill) # 賬單去重

for el in remove_repeat:

time = bill.count(el) # 計算賬單中重複商品的數量

num, name, price = el

print(f"商品: 數量: **:")

price_sum = price_sum + price * time

money = int(money) - price_sum # 剩餘金額

print(f"本次共消費:元\n賬戶餘額為:元\n已清空您的購物車,歡迎您下次光臨!")

break

break

else:

print("輸入金額有誤,請重新輸入!")

模擬購物車

procuct name1 蘋果7 plus 5700 小公尺5 plus 2600 華為p10 4088 錘子m1 2499 魅族6 plus 2999 shopping cart 購物車 shopping name 選購的商品名 expense 0 總消費 首頁print 歡迎來到手機大賣場 s...

Python之模擬購物車

usr bin env python coding utf 8 filename shopping.py time 2020 3 1 14 14 author anqixiang function 模擬購物車功能 1.啟動程式,輸入工資,列印商品列表 2.使用者根據商品編號選擇商品 3.選擇商品後,...

python 之模擬購物車

全部小 在此 product list iphone7 5800 coffee 30 疙瘩湯 10 python book 99 bike 199 vivo x9 2499 shopping cart salary int input input your salary while true men...