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脚本实现网卡流量监控
Feb 14 Python
详解Python中的type()方法的使用
May 21 Python
win10环境下python3.5安装步骤图文教程
Feb 03 Python
python3 打开外部程序及关闭的示例
Nov 06 Python
在Python中,不用while和for循环遍历列表的实例
Feb 20 Python
Python+opencv 实现图片文字的分割的方法示例
Jul 04 Python
TensorFlow获取加载模型中的全部张量名称代码
Feb 11 Python
在 Pycharm 安装使用black的方法详解
Apr 02 Python
基于Python+QT的gui程序开发实现
Jul 03 Python
微软开源最强Python自动化神器Playwright(不用写一行代码)
Jan 05 Python
教你怎么用PyCharm为同一服务器配置多个python解释器
May 31 Python
python常见的占位符总结及用法
Jul 02 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下实现一个阿拉伯数字转中文数字的函数
2008/07/10 PHP
PHP+SQL 注入攻击的技术实现以及预防办法
2011/01/27 PHP
PHP设计模式 注册表模式
2012/02/05 PHP
PHP 第一节 php简介
2012/04/28 PHP
php 启动时报错的简单解决方法
2014/01/27 PHP
5款适合PHP使用的HTML编辑器推荐
2015/07/03 PHP
Firefox中使用outerHTML的2种解决方法
2014/06/07 Javascript
JavaScript中的数组操作介绍
2014/12/30 Javascript
js实现的Easy Tabs选项卡用法实例
2015/09/06 Javascript
JQUERY表单暂存功能插件分享
2016/02/23 Javascript
nodejs入门教程三:调用内部和外部方法示例
2017/04/24 NodeJs
jQuery基于闭包实现的显示与隐藏div功能示例
2018/06/09 jQuery
利用jsonp解决js读取本地json跨域的问题
2018/12/11 Javascript
弱类型语言javascript开发中的一些坑实例小结【变量、函数、数组、对象、作用域等】
2019/08/07 Javascript
js实现烟花特效
2020/03/02 Javascript
微信小程序实现比较功能的方法汇总(五种方法)
2020/03/07 Javascript
微信小程序scroll-view点击项自动居中效果的实现
2020/03/25 Javascript
手把手教您实现react异步加载高阶组件
2020/04/07 Javascript
js正则表达式简单校验方法
2021/01/03 Javascript
[00:30]塑造者的传承礼包-戴泽“暗影之焰”套装展示视频
2014/04/04 DOTA
[01:27]2014DOTA2展望TI 剑指西雅图IG战队专访
2014/06/30 DOTA
Python使用MD5加密字符串示例
2014/08/22 Python
Pycharm学习教程(3) 代码运行调试
2017/05/03 Python
python爬虫入门教程--快速理解HTTP协议(一)
2017/05/25 Python
Python功能点实现:函数级/代码块级计时器
2019/01/02 Python
Python中模块(Module)和包(Package)的区别详解
2019/08/07 Python
CSS3实现彩色进度条动画的示例
2020/10/29 HTML / CSS
压铸汽车模型收藏家:Diecastmodelswholesale.com
2016/12/21 全球购物
小米官方旗舰店:Xiaomi
2020/08/07 全球购物
物流仓储实习自我鉴定
2013/09/25 职场文书
挂职思想汇报
2013/12/31 职场文书
施工安全承诺书
2014/05/22 职场文书
药品开票员岗位职责
2015/04/15 职场文书
2015年小学数学教师工作总结
2015/05/20 职场文书
学生会主席任命书
2015/09/21 职场文书
小学四年级班主任工作经验交流材料
2015/11/02 职场文书