import math
商品的信息字典
dict_commodity_info = {
101: {"name": "屠龍刀", "price": 10000},
102: {"name": "倚天劍", "price": 10000},
103: {"name": "九陰白骨爪", "price": 8000},
104: {"name": "九陽神功", "price": 9000},
105: {"name": "降龍十八掌", "price": 8000},
106: {"name": "乾坤大挪移", "price": 10000}
}
購買物品的列表
list_orders = []
def print_menu():
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
print("1、購買商品")
print("2、商品結算")
print("3、退出系統")
print("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-")
def print_commodity_info():
"""
打印商品信息
:return:
"""
for key, value in dict_commodity_info.items():
print("編號:%d,名稱:%s,單價:%d。" % (key, value["name"], value["price"]))
def choice_commodity():
"""
用戶購買商品
:return: 用戶購買商品編號和數量{"cid": 商品編號, "count": 數量}
"""
while True:
cid = int(input("請輸入商品編號:"))
if cid in dict_commodity_info:
break
else:
print("該商品不存在")
count = int(input("請輸入購買數量:"))
return {"cid": cid, "count": count}
def print_buying_info():
"""
打印用戶購買的商品信息
:return:
"""
for item in list_orders:
commodity_id = dict_commodity_info[item["cid"]]
print("商品:%s,單價:%d,數量:%d." % (commodity_id["name"], commodity_id["price"], item["count"]))
calculate_total_price()
def calculate_total_price():
"""
計算用戶購買商品的總價格
:return: 用戶購買商品的總價格(total_price)
"""
total_price = 0
for item in list_orders:
commodity_id = dict_commodity_info[item["cid"]]
total_price += commodity_id["price"] * item["count"]
return total_price
def print_two_menu():
"""
打印二級菜單
:return:
"""
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
print("1、繼續購物")
print("2、結束購物")
print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
choice_two_menu()
def choice_two_menu():
"""
二級菜單的選擇輸入
:return:
"""
choice = input("請選擇功能>")
if choice == "1":
buying()
elif choice == "2":
exit()
else:
print("輸入錯誤,請重新輸入")
print_two_menu()
def total_prices():
"""
用戶輸入價格,並計算價格
:return:
"""
if calculate_total_price() == 0:
print("你沒有選擇商品,請選擇購買商品")
buying()
else:
while True:
input_money = float(input("總價%d元,請輸入金額:" % calculate_total_price()))
if input_money >= calculate_total_price():
print("購買成功,找回:%d元。" % (input_money - calculate_total_price()))
list_orders.clear()
print_two_menu()
else:
moneys = order_pay_money(input_money)
if moneys >= calculate_total_price():
print("購買成功,找回:%d元。" % (moneys - calculate_total_price()))
list_orders.clear()
print_two_menu()
def order_pay_money(order_input_money):
"""
當第一次支付的金額不夠時,進行第二次支付
:param order_input_money: 第一次支付的錢
:return: 第一次和第二次的總和
"""
total_money = order_input_money
total_moneys = 0
print("金額不足.還需要支付%d元-_-" % (math.fabs(total_money - calculate_total_price())))
two_input_money = float(input("請繼續支付%d元 >" % (math.fabs(total_money - calculate_total_price()))))
total_moneys = total_money + two_input_money
return total_moneys
def buying():
"""
用戶選擇功能主菜單
:return:
"""
while True:
print_menu()
item = input("請選擇功能>")
if item == "1":
print_commodity_info()
list_orders.append(choice_commodity())
print("添加到購物車。")
elif item == "2":
print_buying_info()
total_prices()
elif item == "3":
exit()
if __name__ == "__main__":
buying()
python實現購物車功能和結算功能
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.