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发送邮件实例讲解(python发邮件附件可以使用email模块实现)
Dec 03 Python
Python读取文件内容的三种常用方式及效率比较
Oct 07 Python
MAC中PyCharm设置python3解释器
Dec 15 Python
Python切片操作深入详解
Jul 27 Python
numpy中三维数组中加入元素后的位置详解
Nov 28 Python
python 消费 kafka 数据教程
Dec 21 Python
python3下pygame如何实现显示中文
Jan 11 Python
Python基于network模块制作电影人物关系图
Jun 19 Python
Python魔术方法专题
Jun 19 Python
利用Python判断整数是否是回文数的3种方法总结
Jul 07 Python
Python中with上下文管理协议的作用及用法
Mar 18 Python
Python下载商品数据并连接数据库且保存数据
Mar 31 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
全文搜索和替换
2006/10/09 PHP
MyEclipse常用配置图文教程
2014/09/11 PHP
PHP随机生成唯一HASH值自定义函数
2015/04/20 PHP
最准确的php截取字符串长度函数
2015/10/29 PHP
Date对象格式化函数代码
2010/07/17 Javascript
网站页面自动跳转实现方法PHP、JSP(下)
2010/08/01 Javascript
使用js画图之正弦曲线
2015/01/12 Javascript
JavaScript实现函数返回多个值的方法
2015/06/09 Javascript
BootStrap daterangepicker 双日历控件
2017/06/02 Javascript
实时监控input框,实现输入框与下拉框联动的实例
2018/01/23 Javascript
Vue2.0 给Tab标签页和页面切换过渡添加样式的方法
2018/03/13 Javascript
微信小程序实现循环动画效果
2018/07/16 Javascript
vue.draggable实现表格拖拽排序效果
2018/12/01 Javascript
如何检查一个对象是否为空
2019/04/11 Javascript
简单了解JavaScript sort方法
2019/11/25 Javascript
Node.js 深度调试方法解析
2020/07/28 Javascript
[01:51]DAC趣味视频-如何成为职业选手.mp4
2017/04/02 DOTA
巧用Python装饰器 免去调用父类构造函数的麻烦
2012/05/18 Python
python基于socket实现网络广播的方法
2015/04/29 Python
Python中Numpy包的安装与使用方法简明教程
2018/07/03 Python
Numpy中的mask的使用
2018/07/21 Python
Python中__slots__属性介绍与基本使用方法
2018/09/05 Python
Python实现数据结构线性链表(单链表)算法示例
2019/05/04 Python
程序员的七夕用30行代码让Python化身表白神器
2019/08/07 Python
Django中文件上传和文件访问微项目的方法
2020/04/27 Python
Python脚本实现监听服务器的思路代码详解
2020/05/28 Python
python从ftp获取文件并下载到本地
2020/12/05 Python
英国最出名高街品牌:Forever Unique
2018/02/24 全球购物
中国梦的演讲稿
2014/01/08 职场文书
合作意向书
2014/07/30 职场文书
生产工厂门卫岗位职责
2014/09/26 职场文书
自信主题班会
2015/08/14 职场文书
go 原生http web 服务跨域restful api的写法介绍
2021/04/27 Golang
Java Dubbo框架知识点梳理
2021/06/26 Java/Android
在HTML中引入CSS的几种方式介绍
2021/12/06 HTML / CSS
分享7个 Python 实战项目练习
2022/03/03 Python