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之网站的结构
Oct 24 Python
简单介绍使用Python解析并修改XML文档的方法
Oct 15 Python
Python编程中time模块的一些关键用法解析
Jan 19 Python
Python使用requests及BeautifulSoup构建爬虫实例代码
Jan 24 Python
图解Python变量与赋值
Apr 03 Python
使用pandas把某一列的字符值转换为数字的实例
Jan 29 Python
python 实现多线程下载m3u8格式视频并使用fmmpeg合并
Nov 15 Python
Tensorflow 多线程设置方式
Feb 06 Python
Python numpy多维数组实现原理详解
Mar 10 Python
完美解决jupyter由于无法import新包的问题
May 26 Python
用python读取xlsx文件
Dec 17 Python
使用opencv-python如何打开USB或者笔记本前置摄像头
Jun 21 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中simplexml_load_string使用实例分享
2014/02/13 PHP
php代码审计比较有意思的例子
2014/05/07 PHP
PHP数组相加操作及与array_merge的区别浅析
2016/11/26 PHP
gearman中任务的优先级和返回状态实例分析
2020/02/27 PHP
关于文本限制字数的js代码
2007/04/02 Javascript
JQuery 国际象棋棋盘 实现代码
2009/06/26 Javascript
jQuery.ajax 用户登录验证代码
2010/10/29 Javascript
js 获取和设置css3 属性值的实现方法
2013/05/06 Javascript
Internet Explorer 11 浏览器介绍:别叫我IE
2014/09/28 Javascript
解决js图片加载时出现404的问题
2020/11/30 Javascript
简单谈谈json跨域
2016/03/13 Javascript
jQuery实现上传图片前预览效果功能
2017/08/03 jQuery
浅析为什么a="abc" 不等于 a=new String("abc")
2017/10/25 Javascript
jquery中有哪些api jQuery主要API
2017/11/20 jQuery
js实现把时间戳转换为yyyy-MM-dd hh:mm 格式(es6语法)
2017/12/28 Javascript
vue-cli常用设置总结
2018/02/24 Javascript
微信小程序页面渲染实现方法
2019/11/06 Javascript
js 对象使用的小技巧实例分析
2019/11/08 Javascript
基于vue中的scoped坑点解说
2020/09/04 Javascript
python 从远程服务器下载东西的代码
2013/02/10 Python
Python迭代用法实例教程
2014/09/08 Python
Python基于百度云文字识别API
2018/12/13 Python
python随机在一张图像上截取任意大小图片的方法
2019/01/24 Python
说说如何遍历Python列表的方法示例
2019/02/11 Python
在pytorch中对非叶节点的变量计算梯度实例
2020/01/10 Python
matplotlib之pyplot模块之标题(title()和suptitle())
2021/02/22 Python
W Concept美国:精选全球独立设计师
2017/02/22 全球购物
英国国家美术馆商店:National Gallery
2019/05/01 全球购物
英国领先的酒杯和水晶玻璃器皿制造商:Dartington Crystal
2019/06/23 全球购物
清洁工表扬信
2014/01/08 职场文书
上班玩游戏检讨书
2014/02/07 职场文书
行政工作个人的自我评价
2014/02/13 职场文书
绿化工程实施方案
2014/03/17 职场文书
办公经费申请报告
2015/05/15 职场文书
nginx优化的六点方法
2021/03/31 Servers
Windows server 2016服务器基本设置
2022/08/14 Servers