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开发WebService系列教程之REST,web.py,eurasia,Django
Jun 30 Python
python函数形参用法实例分析
Aug 04 Python
Python实现的爬虫功能代码
Jun 24 Python
用python实现的线程池实例代码
Jan 06 Python
python如何将图片转换为字符图片
Aug 19 Python
Python3实现的简单验证码识别功能示例
May 02 Python
Python 查看list中是否含有某元素的方法
Jun 27 Python
python多线程http压力测试脚本
Jun 25 Python
python多线程并发及测试框架案例
Oct 15 Python
Python values()与itervalues()的用法详解
Nov 27 Python
详解Python 重学requests发起请求的基本方式
Feb 07 Python
pycharm无法导入本地模块的解决方式
Feb 12 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 session 存储方式的详细介绍
2013/06/25 PHP
PHP常用函数和常见疑难问题解答
2014/03/05 PHP
PHP+ajax实现二级联动菜单功能示例
2018/08/10 PHP
javascript动画效果类封装代码
2007/08/28 Javascript
JavaScript 撑出页面文字换行
2009/06/15 Javascript
同时使用n个window onload加载实例介绍
2013/04/25 Javascript
jquery foreach使用示例
2013/09/12 Javascript
javasciprt下jquery函数$.post执行无响应的解决方法
2014/03/13 Javascript
JS组件Bootstrap ContextMenu右键菜单使用方法
2016/04/17 Javascript
BootstrapValidator不触发校验的实现代码
2016/09/28 Javascript
关于 jQuery Easyui异步加载tree的问题解析
2016/12/06 Javascript
基于 Immutable.js 实现撤销重做功能的实例代码
2018/03/01 Javascript
vscode 开发Vue项目的方法步骤
2018/11/25 Javascript
JavaScript函数式编程(Functional Programming)高阶函数(Higher order functions)用法分析
2019/05/22 Javascript
基于js实现抽红包并分配代码实例
2019/09/19 Javascript
vue transition 在子组件中失效的解决
2019/11/12 Javascript
解决Antd 里面的select 选择框联动触发的问题
2020/10/24 Javascript
ant-design-vue 时间选择器赋值默认时间的操作
2020/10/27 Javascript
[47:45]Liquid vs OG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python并发编程之线程实例解析
2017/12/27 Python
Python登录并获取CSDN博客所有文章列表代码实例
2017/12/28 Python
python批量获取html内body内容的实例
2019/01/02 Python
浅析python的Lambda表达式
2019/02/27 Python
python经典趣味24点游戏程序设计
2019/07/26 Python
Python使用matplotlib实现交换式图形显示功能示例
2019/09/06 Python
Python序列化pickle模块使用详解
2020/03/05 Python
Django Auth用户认证组件实现代码
2020/10/13 Python
HTML+CSS3模拟心的跳动实例代码
2017/09/05 HTML / CSS
html5 canvas绘制放射性渐变色效果
2018/01/04 HTML / CSS
Html5 video标签视频的最佳实践
2020/02/26 HTML / CSS
小学生成长感言
2014/01/30 职场文书
因家庭原因离职的辞职信范文
2015/05/12 职场文书
苦儿流浪记读书笔记
2015/07/01 职场文书
公司劳动纪律管理制度
2015/08/04 职场文书
银行岗位培训心得体会
2016/01/09 职场文书
js 数组 fill() 填充方法
2021/11/02 Javascript