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 相关文章推荐
将Django使用的数据库从MySQL迁移到PostgreSQL的教程
Apr 11 Python
Python实现方便使用的级联进度信息实例
May 05 Python
python编程之requests在网络请求中添加cookies参数方法详解
Oct 25 Python
教你用 Python 实现微信跳一跳(Mac+iOS版)
Jan 04 Python
tf.truncated_normal与tf.random_normal的详细用法
Mar 05 Python
python实现微信防撤回神器
Apr 29 Python
Django中的FBV和CBV用法详解
Sep 15 Python
Pytorch 保存模型生成图片方式
Jan 10 Python
Tensorflow:转置函数 transpose的使用详解
Feb 11 Python
学习Python列表的基础知识汇总
Mar 10 Python
python2和python3哪个使用率高
Jun 23 Python
用Python生成会跳舞的美女
Jan 18 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
phpMyAdmin 安装及问题总结
2009/05/28 PHP
PHP中MVC模式的模板引擎开发经验分享
2011/03/23 PHP
php 随机排序广告的实现代码
2011/05/09 PHP
PHP缩略图等比例无损压缩,可填充空白区域补充色
2011/06/10 PHP
zf框架的zend_cache缓存使用方法(zend框架)
2014/03/14 PHP
PHP调试的强悍利器之PHPDBG
2016/02/22 PHP
PHP微信支付实例解析
2016/07/22 PHP
php lcg_value与mt_rand生成0~1随机小数的效果对比分析
2017/04/05 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
PHP实现百度人脸识别
2019/05/06 PHP
wordpress之js库集合研究介绍
2007/08/17 Javascript
cnblogs中在闪存中屏蔽某人的实现代码
2010/11/14 Javascript
js控制CSS样式属性语法对照表
2012/12/11 Javascript
JS 实现导航栏悬停效果(续2)
2013/09/24 Javascript
node.js中的fs.chown方法使用说明
2014/12/16 Javascript
javascript事件模型实例分析
2015/01/30 Javascript
JS实现控制表格行内容垂直对齐的方法
2015/03/30 Javascript
AngularJS基础 ng-init 指令简单示例
2016/08/02 Javascript
Three.js利用dat.GUI如何简化试验流程详解
2017/09/26 Javascript
JS非行间样式获取函数的实例代码
2018/06/05 Javascript
ant-design-vue 快速避坑指南(推荐)
2020/01/21 Javascript
JS+canvas五子棋人机对战实现步骤详解
2020/06/04 Javascript
[03:48]DOTA2完美大师赛主赛事第二日精彩集锦
2017/11/24 DOTA
python读取目录下最新的文件夹方法
2018/12/24 Python
Python函数返回不定数量的值方法
2019/01/22 Python
Python 实现加密过的PDF文件转WORD格式
2020/02/04 Python
TensorFlow:将ckpt文件固化成pb文件教程
2020/02/11 Python
python由已知数组快速生成新数组的方法
2020/04/08 Python
python实现学生通讯录管理系统
2021/02/25 Python
时尚孕妇装:Ingrid & Isabel
2019/05/08 全球购物
Janie and Jack美国官网:GAP旗下的高档童装品牌
2019/09/09 全球购物
求最大连续递增数字串(如"ads3sl456789DF3456ld345AA"中的"456789")
2015/09/11 面试题
大学同学聚会邀请函
2014/01/19 职场文书
快餐店的创业计划书范文
2014/01/29 职场文书
React-vscode使用jsx语法的问题及解决方法
2021/06/21 Javascript
带你了解CSS基础知识,样式
2021/07/21 HTML / CSS