python如何实现购物车功能
小编给大家分享一下python如何实现购物车功能,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!
要求:
1、启动程序后让用户输入余额,并打印商品列表
2、用户通过输入编号购买商品
3、用户选择商品购买后,根据余额判断成功或者失败,给出对应提示
4、可以随时退出,退出后打印账号余额以及购买的商品列表
思路:
1、首先,用户余额需要进行存储,用户购买的物品需要进行存储在数组中
2、用户购买成功后,将购买的物品放入物品集合,并用总金额减去余额
3、如果失败,给出失败提示,并打印余额
4、用户选择继续后,无论成功失败,都可以继续购买
代码实现:
#用户输入工资balance=int(input("Pleaseinputbalance:"))#定义衣服的数组clothes=[["pants",100],["T-shirt",50],["skirt",20]]#个人所得,包括金钱和获取的物品haveGoods=[balance,[]]flag=Truewhileflag:#打印衣服列表print("Theclotheslistisasfollows")print("______clothesList______")i=1;forcinclothes:print('Thenumber:',i,":",c)i+=1#用户输入商品编号code=int(input("Pleasechoosethenumber:"))#判断钱是否够用ifclothes[code-1][1]<=haveGoods[0]:#在自己的购物清单中加入已购物品haveGoods[1].append(clothes[code-1])#减去花费的金钱haveGoods[0]-=clothes[code-1][1]print("Youhavesuccessfullypurchased!")print("Youraccountbalanceis:",haveGoods[0])else:print("Youraccountbalanceisinsufficient!")print("Youraccountbalanceis:",haveGoods[0])judge=input("Youcanpressanybuttontocontinue,orinput'n'toleave:")ifjudge=="n":flag=Falseprint("Youraccountbalanceis:",haveGoods[0])print("Yourshoppinglistisasfollows:")print("______clothesList______")forhinhaveGoods[1]:print(h)
看完了这篇文章,相信你对python如何实现购物车功能有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。