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类的基础入门知识
Nov 24 Python
paramiko模块安装和使用(远程登录服务器)
Jan 27 Python
python创建进程fork用法
Jun 04 Python
Django中的CACHE_BACKEND参数和站点级Cache设置
Jul 23 Python
Django中使用celery完成异步任务的示例代码
Jan 23 Python
Python wxpython模块响应鼠标拖动事件操作示例
Aug 23 Python
基于python全局设置id 自动化测试元素定位过程解析
Sep 04 Python
使用pyshp包进行shapefile文件修改的例子
Dec 06 Python
Python 实现敏感目录扫描的示例代码
May 21 Python
tensorflow之读取jpg图像长和宽实例
Jun 18 Python
Python 连接 MySQL 的几种方法
Sep 09 Python
python 实现简易的记事本
Nov 30 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+JS三级菜单联动菜单实现方法
2016/02/24 PHP
Yii框架表单模型和验证用法
2016/05/20 PHP
PHP与jquery实时显示网站在线人数实例详解
2016/12/02 PHP
Windows下php+mysql5.7配置教程
2017/05/16 PHP
PHP经典设计模式之依赖注入定义与用法详解
2019/05/21 PHP
javascript显示选择目录对话框的代码
2008/11/10 Javascript
如何判断元素是否为HTMLElement元素
2013/12/06 Javascript
JQuery选中checkbox方法代码实例(全选、反选、全不选)
2015/04/27 Javascript
jQuery模拟黑客帝国矩阵效果实例
2015/06/28 Javascript
js判断日期时间有效性的方法
2015/10/24 Javascript
JQuery日历插件My97DatePicker日期范围限制
2016/01/20 Javascript
详解nodejs异步I/O和事件循环
2017/06/07 NodeJs
微信小程序动态显示项目倒计时效果
2017/06/13 Javascript
JS HTML图片显示Canvas 压缩功能
2017/07/21 Javascript
基于 D3.js 绘制动态进度条的实例详解
2018/02/26 Javascript
nodejs和react实现即时通讯简易聊天室功能
2019/08/21 NodeJs
vue 单页应用和多页应用的优劣
2020/10/22 Javascript
[03:44]2015国际邀请赛选手档案—Cloud9.NoTail
2015/07/28 DOTA
[00:47]TI7不朽珍藏III——沙王不朽展示
2017/07/15 DOTA
[42:35]2018DOTA2亚洲邀请赛3月30日 小组赛A组 VG VS OpTic
2018/03/31 DOTA
跟老齐学Python之编写类之三子类
2014/10/11 Python
Python的Flask框架中使用Flask-Migrate扩展迁移数据库的教程
2016/06/14 Python
Python的CGIHTTPServer交互实现详解
2018/02/08 Python
python实现泊松图像融合
2018/07/26 Python
python获取磁盘号下盘符步骤详解
2019/06/19 Python
pandas 像SQL一样使用WHERE IN查询条件说明
2020/06/05 Python
伊利莎白雅顿官网:Elizabeth Arden
2016/10/10 全球购物
精灵市场:Pixie Market
2019/06/18 全球购物
造型师求职自荐信
2013/09/27 职场文书
统计系教授推荐信
2014/02/28 职场文书
竞选学习委员演讲稿
2014/09/01 职场文书
党的群众路线批评与自我批评发言稿
2014/10/16 职场文书
2015年幼儿园教育教学工作总结
2015/05/25 职场文书
python实现黄金分割法的示例代码
2021/04/28 Python
详解PHP服务器如何在有限的资源里最大提升并发能力
2021/05/25 PHP
SpringBoot 整合mongoDB并自定义连接池的示例代码
2022/02/28 MongoDB