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 psutil模块简单使用实例
Apr 28 Python
python获取当前日期和时间的方法
Apr 30 Python
举例讲解Python设计模式编程中对抽象工厂模式的运用
Mar 02 Python
Pycharm远程调试openstack的方法
Nov 21 Python
Python之Scrapy爬虫框架安装及简单使用详解
Dec 22 Python
VScode编写第一个Python程序HelloWorld步骤
Apr 06 Python
python调用API实现智能回复机器人
Apr 10 Python
python 日志增量抓取实现方法
Apr 28 Python
Tensorflow加载预训练模型和保存模型的实例
Jul 27 Python
python实现flappy bird小游戏
Dec 24 Python
对Pyhon实现静态变量全局变量的方法详解
Jan 11 Python
python中操作文件的模块的方法总结
Feb 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一些有意思的小区别
2006/12/06 PHP
PHP基于mssql扩展远程连接MSSQL的简单实现方法
2016/10/08 PHP
PHP7引入的"??"和"?:"的区别讲解
2019/04/08 PHP
详解PHP 7.4 中数组延展操作符语法知识点
2019/07/19 PHP
php ZipArchive实现多文件打包下载实例
2019/10/31 PHP
javascript支持firefox,ie7页面布局拖拽效果代码
2007/12/20 Javascript
浅谈jQuery中 wrap() wrapAll() 与 wrapInner()的差异
2014/11/12 Javascript
javascript生成随机数方法汇总
2015/11/12 Javascript
JavaScript高级教程5.6之基本包装类型(详细)
2015/11/23 Javascript
javascript从定义到执行 你不知道的那些事
2016/01/04 Javascript
javascript中arguments,callee,caller详解
2016/03/16 Javascript
Bootstrap select实现下拉框多选效果
2016/12/23 Javascript
Bootstrap超大屏幕的实现代码
2017/03/22 Javascript
JS判断时间段的实现代码
2017/06/14 Javascript
vue component组件使用方法详解
2017/07/14 Javascript
JavaScript引用类型Object常见用法实例分析
2018/08/08 Javascript
vue实现压缩图片预览并上传功能(promise封装)
2019/01/10 Javascript
说说如何利用 Node.js 代理解决跨域问题
2019/04/22 Javascript
教你完全理解ReentrantLock重入锁
2019/06/03 Javascript
vue-router两种模式区别及使用注意事项详解
2019/08/01 Javascript
移动端手指操控左右滑动的菜单
2019/09/08 Javascript
vue移动端使用canvas签名的实现
2020/01/15 Javascript
vue实现折线图 可按时间查询
2020/08/21 Javascript
Python 变量类型及命名规则介绍
2013/06/08 Python
Python自动调用IE打开某个网站的方法
2015/06/03 Python
Python datetime和unix时间戳之间相互转换的讲解
2019/04/01 Python
django restframework serializer 增加自定义字段操作
2020/07/15 Python
如何一键升级Python所有包
2020/11/05 Python
python使用正则表达式匹配txt特定字符串(有换行)
2020/12/09 Python
PUMA官方商城:世界领先的运动品牌之一
2016/11/16 全球购物
医院护士求职自荐信格式
2013/09/21 职场文书
自荐信的五个重要部分
2013/10/29 职场文书
仓库规划计划书
2014/04/28 职场文书
我的理想演讲稿
2014/04/30 职场文书
体育专业求职信
2014/07/16 职场文书
2015国庆节66周年标语
2015/07/30 职场文书