失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 模拟购物车购物过程python 用函数模拟简单的购物车(Python)

模拟购物车购物过程python 用函数模拟简单的购物车(Python)

时间:2022-06-28 02:17:46

相关推荐

模拟购物车购物过程python 用函数模拟简单的购物车(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

如果觉得《模拟购物车购物过程python 用函数模拟简单的购物车(Python)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。