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实现目录树生成示例
Mar 28 Python
Python中使用Tkinter模块创建GUI程序实例
Jan 14 Python
python编写简单爬虫资料汇总
Mar 22 Python
python实现基于SVM手写数字识别功能
May 27 Python
使用python中的in ,not in来检查元素是不是在列表中的方法
Jul 06 Python
Python实用技巧之列表、字典、集合中根据条件筛选数据详解
Jul 11 Python
Python编程中类与类的关系详解
Aug 08 Python
Django+boostrap 美化admin后台的操作
Mar 11 Python
python矩阵运算,转置,逆运算,共轭矩阵实例
May 11 Python
能让Python提速超40倍的神器Cython详解
Jun 24 Python
Python利用FlashText算法实现替换字符串
Mar 31 Python
Python 图片添加美颜效果
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
第五节 克隆 [5]
2006/10/09 PHP
ThinkPHP跳转页success及error模板实例教程
2014/07/17 PHP
php根据一个给定范围和步进生成数组的方法
2015/06/19 PHP
PHP实现简单爬虫的方法
2015/07/29 PHP
CodeIgniter集成smarty的方法详解
2016/05/26 PHP
Ctrl+Enter提交内容信息
2006/06/26 Javascript
Javascript 写的简单进度条控件
2008/01/22 Javascript
javascript预加载图片、css、js的方法示例介绍
2013/10/14 Javascript
jQuery+ajax实现鼠标单击修改内容的方法
2014/06/27 Javascript
Javascript6中字符串的四个新用法分享
2016/09/11 Javascript
Angular6 Filter实现页面搜索的示例代码
2018/12/02 Javascript
JavaScript实现公告栏上下滚动效果
2020/03/13 Javascript
JS Array.from()将伪数组转换成数组的方法示例
2020/03/23 Javascript
Vue实现摇一摇功能(兼容ios13.3以上)
2021/01/26 Vue.js
[30:51]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#1Liquid VS MVP.Phx第一局
2016/03/04 DOTA
利用Python实现颜色色值转换的小工具
2016/10/27 Python
浅谈django中的认证与登录
2016/10/31 Python
Python用threading实现多线程详解
2017/02/03 Python
python实现折半查找和归并排序算法
2017/04/14 Python
Python设计模式之命令模式简单示例
2018/01/10 Python
python机器人行走步数问题的解决
2018/01/29 Python
解决PyCharm不运行脚本,而是运行单元测试的问题
2019/01/17 Python
利用python实现对web服务器的目录探测的方法
2019/02/26 Python
在Python中合并字典模块ChainMap的隐藏坑【推荐】
2019/06/27 Python
python中@property和property函数常见使用方法示例
2019/10/21 Python
python实现图片横向和纵向拼接
2020/03/05 Python
Python经纬度坐标转换为距离及角度的实现
2020/11/01 Python
Zalando Lounge瑞士:时尚与生活方式购物俱乐部
2020/03/12 全球购物
通信工程毕业生自荐信
2013/11/01 职场文书
告诉你怎样写创业计划书
2014/01/27 职场文书
关于读书的演讲稿600字
2014/08/27 职场文书
优秀教师个人总结
2015/02/11 职场文书
2015年骨干教师工作总结
2015/05/26 职场文书
漫画「日和酱的要求是绝对的」第3卷封面公开
2022/03/21 日漫
Python四款GUI图形界面库介绍
2022/06/05 Python
ubuntu如何搭建vsftpd服务器
2022/12/24 Servers