Python实现购物车程序
本文实例为大家分享了程序:Python购物车程序,具体内容如下
需求:
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
- 如余额不足,可充值
代码:
#coding=utf-8 #Version:python3.6.0 #Tools:Pycharm2017.3.2 _date_='2018/4/16/01614:50' _author_='Hongyong' salary=int(input("Pleaseinputyoursalary:")) shoppingmart=[] items=(["1","Huawei","¥",2800], ["2","Earphone","¥",300], ["3","Book","¥",80]) msg_items=''' ----------items---------- 1.Huawei¥2800 2.Earphone¥300 3.Book¥80 ------------------------- ''' print(msg_items) whileTrue: shopindex=int(input("Pleasechoosegoods:")) ifsalary>items[shopindex-1][3]: shoppingmart.append(items[shopindex-1]) salary-=int(items[shopindex-1][3]) print("Youhavebought{name}!".format(name=items[shopindex-1][1])) print("Yourbalanceis:¥",salary) decision=input("Doyouwanttoquitnow?") print(msg_items) else: print("Yourbalanceisnotenough!Pleasetrysthelse.") recharge_ans=input("Doyouwanttorecharge?") ifrecharge_ans=="y": recharge=int(input("Pleaseinputmoney:")) print("Pleasewaitforawhile...") salary+=recharge print("Youhaverechargedsuccessfully!") print("Andthebalanceis:",salary,"now!") decision=input("Doyouwanttoquitnow?") print(msg_items) ifdecision=="q": break else: continue print("Youhavebought:",shoppingmart) print("Yourbalanceis:¥",salary) print("Welcomeyournextcoming!")
程序效果:
Pleaseinputyoursalary:0 ----------items---------- 1.Huawei¥2800 2.Earphone¥300 3.Book¥80 ------------------------- Pleasechoosegoods:1 Yourbalanceisnotenough!Pleasetrysthelse. Doyouwanttorecharge?y Pleaseinputmoney:30000 Pleasewaitforawhile... Youhaverechargedsuccessfully! Andthebalanceis:30000now! Doyouwanttoquitnow? ----------items---------- 1.Huawei¥2800 2.Earphone¥300 3.Book¥80 ------------------------- Pleasechoosegoods:1 YouhaveboughtHuawei! Yourbalanceis:¥27200 Doyouwanttoquitnow? ----------items---------- 1.Huawei¥2800 2.Earphone¥300 3.Book¥80 ------------------------- Pleasechoosegoods:2 YouhaveboughtEarphone! Yourbalanceis:¥26900 Doyouwanttoquitnow?q ----------items---------- 1.Huawei¥2800 2.Earphone¥300 3.Book¥80 ------------------------- Youhavebought:[['1','Huawei','¥',2800],['2','Earphone','¥',300]] Yourbalanceis:¥26900 Welcomeyournextcoming!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。