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项目
Jul 15 Python
Python方法的延迟加载的示例代码
Dec 18 Python
Python多线程扫描端口代码示例
Feb 09 Python
pycharm安装和首次使用教程
Aug 27 Python
利用python循环创建多个文件的方法
Oct 25 Python
Python之使用adb shell命令启动应用的方法详解
Jan 07 Python
Python实现从SQL型数据库读写dataframe型数据的方法【基于pandas】
Mar 18 Python
Python实现使用request模块下载图片demo示例
May 24 Python
对python中return与yield的区别详解
Mar 12 Python
Python获取浏览器窗口句柄过程解析
Jul 25 Python
【超详细】八大排序算法的各项比较以及各自特点
Mar 31 Python
详解PyTorch模型保存与加载
Apr 28 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
mysqli_set_charset和SET NAMES使用抉择及优劣分析
2013/01/13 PHP
探讨如何在PHP开启gzip页面压缩实例
2013/06/09 PHP
PHP中HTML标签过滤技巧
2014/01/07 PHP
PHP实现数组array转换成xml的方法
2016/07/19 PHP
PHP GD库相关图像生成和处理函数小结
2016/09/30 PHP
php mysql_list_dbs()函数用法示例
2017/03/29 PHP
php报错502badgateway解决方法
2019/10/11 PHP
4种Windows系统下Laravel框架的开发环境安装及部署方法详解
2020/04/06 PHP
ajax中get和post的说明及使用与区别
2012/12/23 Javascript
JQuery获取或设置ckeditor的数据(示例代码)
2013/11/15 Javascript
基于jQuery实现下拉框
2014/11/24 Javascript
jQuery插件zTree实现获取当前选中节点在同级节点中序号的方法
2017/03/08 Javascript
解决ionic和angular上拉加载的问题
2017/08/03 Javascript
NodeJS爬虫实例之糗事百科
2017/12/14 NodeJs
Vue 组件注册实例详解
2019/02/23 Javascript
javascript设计模式 ? 备忘录模式原理与用法实例分析
2020/04/21 Javascript
[02:52]2017DOTA2国际邀请赛中国区预选赛晋级之路
2017/07/03 DOTA
python爬虫_自动获取seebug的poc实例
2017/08/05 Python
python 异或加密字符串的实例
2018/10/14 Python
Python学习笔记之变量、自定义函数用法示例
2019/05/28 Python
UI自动化定位常用实现方法代码示例
2020/10/27 Python
Prometheus开发中间件Exporter过程详解
2020/11/30 Python
Rockport乐步美国官网:风靡美国的白宫鞋
2016/11/24 全球购物
英国最大的汽车配件在线商店:Euro Car Parts
2019/09/30 全球购物
毕业生求职简历的自我评价
2013/10/23 职场文书
医药专业应届毕业生求职信范文
2014/01/01 职场文书
成绩单公证书
2014/04/10 职场文书
供货协议书范本
2014/04/22 职场文书
机关干部个人对照检查材料思想汇报
2014/09/28 职场文书
复兴之路纪录片观后感
2015/06/02 职场文书
民主生活会意见
2015/06/05 职场文书
反邪教教育心得体会
2016/01/15 职场文书
2016年世界人口日宣传活动总结
2016/04/05 职场文书
2019辞职报告范本3篇!
2019/07/23 职场文书
mysql多表查询-笔记七
2021/04/05 MySQL
SQL实现LeetCode(177.第N高薪水)
2021/08/04 MySQL