用函数模拟简单的购物车(Python)
""" 购物车功能: a.引导用户输入金额 b.给用户展示所有的商品 c.引导用户输入需要进行的操作【添加 删除 结算购物车 退出】 d.引导用户选择商品 e.引导用户输入需要购买的商品数量 f.添加到购物车【容器】 g.整个循环的操作,循环的次数不确定 """ def add(): print(super_market) # 超市商品列表 print(price_list) #商品价格表 commodity = input("please input you need to add to your shopping basket:") #请输入你需要添加的商品 commodity_number = int(input("please input the number of commodity:")) #请输入你需要添加的商品数量 user_market[commodity] = commodity_number #将商品加到用户购物车中 super_market[commodity] -= commodity_number return def dell(): dell_name = input("please input you del name :") # 移除的商品名称 dell_number = int(input("please the number you del:")) #移除的商品的数量 user_market[dell_name] -=dell_number #将购物车中的商品移除 super_market[dell_name] += dell_number #商品放回后超市现有的商品数目 print(user_market) return def looking(): print(super_market) print(user_market) return def settlement(): amout = 0 print(user_market) for key in user_market: amout += user_market[key] * price_list[key] print(amout) if amout > money_user: #如果总价高于所带的金额,退出循环 print("you money is not pay for,please take the full money!") return False def exit(): print("Thank you for your presence and welcome to come again next time!!") return False money_user = int(input("please user input your take money:")) super_market = {"apple":50,"watermelon":20,"nuddles":40,"drinks":30} price_list = {"apple":2,"watermelon":10,"nuddles":6,"drinks":3} user_market = {} while True: operation = int(input("please input your operation\n(1.add****2.del****3.looking****4.settlement****5.exit) :")) if operation == 1: add() elif operation ==2: dell() elif operation ==3: looking() elif operation ==4: settlement() if settlement() == False: break elif operation ==5: exit() if exit() == False: break