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列表生成器的循环技巧分享
Mar 06 Python
python实现简单ftp客户端的方法
Jun 28 Python
基于python爬虫数据处理(详解)
Jun 10 Python
Python机器学习之SVM支持向量机
Dec 27 Python
Python实现将照片变成卡通图片的方法【基于opencv】
Jan 17 Python
python装饰器深入学习
Apr 06 Python
浅谈numpy生成数组的零值问题
Nov 12 Python
Python学习笔记之读取文件、OS模块、异常处理、with as语法示例
Jun 04 Python
Python代码生成视频的缩略图的实例讲解
Dec 22 Python
执行Python程序时模块报错问题
Mar 26 Python
pytorch随机采样操作SubsetRandomSampler()
Jul 07 Python
Pycharm新手使用教程(图文详解)
Sep 17 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
header()函数使用说明
2006/11/23 PHP
PHP 获取客户端真实IP地址多种方法小结
2010/05/15 PHP
Thinkphp中的curd应用实用要点
2015/01/04 PHP
php文件上传 你真的掌握了吗
2016/11/28 PHP
关于php 高并发解决的一点思路
2017/04/16 PHP
PHP使用file_get_contents发送http请求功能简单示例
2018/04/29 PHP
PHP中soap用法示例【SoapServer服务端与SoapClient客户端编写】
2018/12/25 PHP
TP5框架安全机制实例分析
2020/04/05 PHP
html数组字符串拼接的最快方法
2009/09/16 Javascript
GRID拖拽行的实例代码
2013/07/18 Javascript
JavaScript+Java实现HTML页面转为PDF文件保存的方法
2016/05/30 Javascript
jQuery监听浏览器窗口大小的变化实例
2017/02/07 Javascript
JavaScript实现设置默认日期范围为最近40天的方法分析
2017/07/12 Javascript
JavaScript获取移动设备型号的实现代码(JS获取手机型号和系统)
2018/03/10 Javascript
JavaScript"模拟事件"的注意要点详解
2019/02/13 Javascript
整理 node-sass 安装失败的原因及解决办法(小结)
2020/02/19 Javascript
[01:20]2018DOTA2亚洲邀请赛总决赛战队LGD晋级之路
2018/04/07 DOTA
Python sys.argv用法实例
2015/05/28 Python
一文总结学习Python的14张思维导图
2017/10/17 Python
python实现windows壁纸定期更换功能
2019/01/21 Python
Python获取网段内ping通IP的方法
2019/01/31 Python
Django如何实现上传图片功能
2019/08/16 Python
python闭包、深浅拷贝、垃圾回收、with语句知识点汇总
2020/03/11 Python
CSS3实现精美横向滚动菜单按钮
2017/04/14 HTML / CSS
CSS3实现文字描边的2种方法(小结)
2020/02/14 HTML / CSS
《小松树和大松树》教学反思
2014/02/20 职场文书
论群众路线学习心得体会
2014/10/31 职场文书
终止劳动合同证明书样本
2014/11/19 职场文书
营销计划书范文
2015/01/17 职场文书
努力学习保证书
2015/02/26 职场文书
政审证明范文
2015/06/19 职场文书
单位病假条范文
2015/08/17 职场文书
用Python提取PDF表格的方法
2021/04/11 Python
Pygame Event事件模块的详细示例
2021/11/17 Python
Python3使用Qt5来实现简易的五子棋小游戏
2022/05/02 Python
nginx之内存池的实现
2022/06/28 Servers