Python实现购物车程序


Posted in Python onApril 16, 2018

本文实例为大家分享了程序:Python购物车程序,具体内容如下

需求:

  • 启动程序后,让用户输入工资,然后打印商品列表
  • 允许用户根据商品编号购买商品
  • 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
  • 可随时退出,退出时,打印已购买商品和余额
  • 如余额不足,可充值 

代码:

#coding=utf-8
#Version:python 3.6.0
#Tools:Pycharm 2017.3.2
_date_ = '2018/4/16/016 14:50'
_author_ = 'Hongyong'

salary = int(input("Please input your salary: "))
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)
while True:
  shopindex = int(input("Please choose goods: "))
  if salary > items[shopindex-1][3]:
    shoppingmart.append(items[shopindex-1])
    salary -= int(items[shopindex-1][3])
    print("You have bought {name} !".format(name = items[shopindex-1][1]))
    print("Your balance is: ¥",salary)
    decision = input("Do you want to quit now?")
    print(msg_items)
  else:
    print("Your balance is not enough! Please try sth else.")
    recharge_ans = input("Do you want to recharge?")
    if recharge_ans == "y":
      recharge = int(input("Please input money: "))
      print("Please wait for a while...")
      salary += recharge
      print("You have recharged successfully!")
      print("And the balance is: ",salary,"now!")
    decision = input("Do you want to quit now?")
    print(msg_items)
  if decision == "q":
    break
  else:
    continue
print("You have bought: ",shoppingmart)
print("Your balance is: ¥",salary)
print("Welcome your next coming!")

程序效果:

Please input your salary: 0
 
----------items----------
1. Huawei   ¥ 2800
2. Earphone  ¥ 300
3. Book    ¥ 80
-------------------------
 
Please choose goods: 1
Your balance is not enough! Please try sth else.
Do you want to recharge?y
Please input money: 30000
Please wait for a while...
You have recharged successfully!
And the balance is: 30000 now!
Do you want to quit now?
 
----------items----------
1. Huawei   ¥ 2800
2. Earphone  ¥ 300
3. Book    ¥ 80
-------------------------
 
Please choose goods: 1
You have bought Huawei !
Your balance is: ¥ 27200
Do you want to quit now?
 
----------items----------
1. Huawei   ¥ 2800
2. Earphone  ¥ 300
3. Book    ¥ 80
-------------------------
 
Please choose goods: 2
You have bought Earphone !
Your balance is: ¥ 26900
Do you want to quit now?q
 
----------items----------
1. Huawei   ¥ 2800
2. Earphone  ¥ 300
3. Book    ¥ 80
-------------------------
 
You have bought: [['1', 'Huawei', '¥', 2800], ['2', 'Earphone', '¥', 300]]
Your balance is: ¥ 26900
Welcome your next coming!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中time模块和datetime模块的用法示例
Feb 28 Python
Python生成器定义与简单用法实例分析
Apr 30 Python
python中pip的安装与使用教程
Aug 10 Python
使用python获取电脑的磁盘信息方法
Nov 01 Python
Python生成rsa密钥对操作示例
Apr 26 Python
python使用pandas处理大数据节省内存技巧(推荐)
May 05 Python
python 模拟贷款卡号生成规则过程解析
Aug 30 Python
Python3和pyqt5实现控件数据动态显示方式
Dec 13 Python
Python实现Keras搭建神经网络训练分类模型教程
Jun 12 Python
Python为何不支持switch语句原理详解
Oct 21 Python
python安装sklearn模块的方法详解
Nov 28 Python
关于python类SortedList详解
Sep 04 Python
神经网络(BP)算法Python实现及应用
Apr 16 #Python
python读取视频流提取视频帧的两种方法
Oct 22 #Python
python读取和保存视频文件
Apr 16 #Python
Python读取视频的两种方法(imageio和cv2)
Apr 15 #Python
python2.7实现FTP文件下载功能
Apr 15 #Python
python实现多线程网页下载器
Apr 15 #Python
Python实现定时精度可调节的定时器
Apr 15 #Python
You might like
php不用正则验证真假身份证
2013/11/06 PHP
ThinkPHP中url隐藏入口文件后接收alipay传值的方法
2014/12/09 PHP
thinkPHP数据库增删改查操作方法实例详解
2016/12/06 PHP
PHP常用函数总结(180多个)
2016/12/25 PHP
PHP Swoole异步Redis客户端实现方法示例
2019/10/24 PHP
Android中资源文件(非代码部分)的使用概览
2012/12/18 Javascript
jquery中获取元素里某一特定子元素的代码
2014/12/02 Javascript
JavaScript常用的弹出广告及背投广告实现方法
2015/02/06 Javascript
jQuery插件实现文件上传功能(支持拖拽)
2020/08/27 Javascript
Angular中$state.go页面跳转并传递参数的方法
2017/05/09 Javascript
JavaScript之Canvas_动力节点Java学院整理
2017/07/04 Javascript
vue组件实现弹出框点击显示隐藏效果
2020/10/26 Javascript
Vue.js 父子组件通信的十种方式
2018/10/30 Javascript
JavaScript深入V8引擎以及编写优化代码的5个技巧
2019/06/24 Javascript
js实现简单页面全屏
2019/09/17 Javascript
在vue中封装方法以及多处引用该方法详解
2020/08/14 Javascript
[04:40]2016个国际邀请赛中国区预选赛场地——华西村观战指南
2016/06/25 DOTA
12步入门Python中的decorator装饰器使用方法
2016/06/20 Python
Python实现通过文件路径获取文件hash值的方法
2017/04/29 Python
Python探索之pLSA实现代码
2017/10/25 Python
Python调用服务接口的实例
2019/01/03 Python
python write无法写入文件的解决方法
2019/01/23 Python
python3.x提取中文的正则表达式示例代码
2019/07/23 Python
Python3显示当前时间、计算时间差及时间加减法示例代码
2019/09/07 Python
python制作朋友圈九宫格图片
2019/11/03 Python
解决pytorch报错:AssertionError: Invalid device id的问题
2020/01/10 Python
通过自学python能找到工作吗
2020/06/21 Python
印度尼西亚在线时尚购物网站:ZALORA印尼
2016/08/02 全球购物
Nike香港官网:Nike HK
2019/03/23 全球购物
南非最大的在线时尚商店:Zando
2019/07/21 全球购物
美德好少年主要事迹
2014/01/29 职场文书
毕业设计致谢词
2015/05/14 职场文书
总经理2015中秋节致辞
2015/07/29 职场文书
nginx处理http请求实现过程解析
2021/03/31 Servers
为什么在foreach循环中JAVA集合不能添加或删除元素
2021/06/11 Java/Android
Mysql中调试存储过程最简单的方法
2021/06/30 MySQL