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让图片按照exif信息里的创建时间进行排序的方法
Mar 16 Python
python 对txt中每行内容进行批量替换的方法
Jul 11 Python
Python3基础教程之递归函数简单示例
Jun 07 Python
python跳出双层for循环的解决方法
Jun 24 Python
python中eval与int的区别浅析
Aug 11 Python
matlab 计算灰度图像的一阶矩,二阶矩,三阶矩实例
Apr 22 Python
Python简单实现词云图代码及步骤解析
Jun 04 Python
Python 实现一个计时器
Jul 28 Python
一文详述 Python 中的 property 语法
Sep 01 Python
Python常用GUI框架原理解析汇总
Dec 07 Python
解决hive中导入text文件遇到的坑
Apr 07 Python
如何在Python项目中引入日志
May 31 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 addslashes 函数详细分析说明
2009/06/23 PHP
PHP用mysql数据库存储session的代码
2010/03/05 PHP
php调用新浪短链接API的方法
2014/11/08 PHP
php实现的一段简单概率相关代码
2016/05/30 PHP
Add Formatted Data to a Spreadsheet
2007/06/12 Javascript
一些技巧性实用js代码小结
2009/10/14 Javascript
javascript json2 使用方法
2010/03/16 Javascript
JSQL 基于客户端的成绩统计实现方法
2010/05/05 Javascript
如何获取select下拉框的值(option没有及有value属性)
2013/11/08 Javascript
showModelDialog弹出文件下载窗口的使用示例
2013/11/19 Javascript
使用jquery实现IE下按backspace相当于返回操作
2014/03/18 Javascript
JQuery中attr方法和removeAttr方法用法实例
2015/05/18 Javascript
javascript回到顶部特效
2016/07/30 Javascript
jquery注册文本框获取焦点清空,失去焦点赋值的简单实例
2016/09/08 Javascript
Vue 过渡(动画)transition组件案例详解
2017/01/22 Javascript
JavaScript禁止微信浏览器下拉回弹效果
2017/05/16 Javascript
浅谈JS 数字和字符串之间相互转化的纠纷
2017/10/20 Javascript
webpack搭建vue 项目的步骤
2017/12/27 Javascript
微信小程序实现美团菜单
2018/06/06 Javascript
Python统计python文件中代码,注释及空白对应的行数示例【测试可用】
2018/07/25 Python
Python3利用scapy局域网实现自动多线程arp扫描功能
2021/01/21 Python
python线程优先级队列知识点总结
2021/02/28 Python
男女时尚与复古风格在线购物:RoseGal(全球免费送货)
2017/07/19 全球购物
蛋白质世界:Protein World
2017/11/23 全球购物
高等教育学自荐书范文
2014/02/10 职场文书
党的群众路线教育实践活动批评与自我批评
2014/02/16 职场文书
材料员岗位职责
2014/03/13 职场文书
百年校庆节目主持词
2014/03/27 职场文书
经济贸易系毕业生求职信
2014/05/31 职场文书
课内比教学心得体会
2014/09/09 职场文书
局机关干部群众路线个人对照检查材料思想汇报
2014/10/05 职场文书
销售经理工作失职检讨书
2014/10/24 职场文书
打架赔偿协议书范本
2014/10/26 职场文书
男生贾里读书笔记
2015/06/30 职场文书
小学庆六一主持词
2015/06/30 职场文书
python接口测试返回数据为字典取值方式
2022/02/12 Python