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动态监控日志内容的示例
Feb 16 Python
python让图片按照exif信息里的创建时间进行排序的方法
Mar 16 Python
Python对象转JSON字符串的方法
Apr 27 Python
Python自然语言处理之词干,词形与最大匹配算法代码详解
Nov 16 Python
python判断一个数是否能被另一个整数整除的实例
Dec 12 Python
深入了解Python枚举类型的相关知识
Jul 09 Python
python与C、C++混编的四种方式(小结)
Jul 15 Python
Numpy的简单用法小结
Aug 28 Python
用Python绘制漫步图实例讲解
Feb 26 Python
利用Python计算KS的实例详解
Mar 03 Python
基于python实现坦克大战游戏
Oct 27 Python
python绘制箱型图
Apr 27 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学习之数组的定义和填充
2011/04/17 PHP
memcache命令启动参数中文解释
2014/01/13 PHP
PHPMailer的主要功能特点和简单使用说明
2014/02/17 PHP
异步加载技术实现当滚动条到最底部的瀑布流效果
2014/09/16 PHP
10个对初学者非常有用的PHP技巧
2016/04/06 PHP
php下载文件,添加响应头的简单实例
2016/09/22 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
2020/02/15 PHP
不错的asp中显示新闻的功能
2006/10/13 Javascript
地址栏传递中文参数乱码在js里用escape转码
2013/08/28 Javascript
JS实现判断滚动条滚到页面底部并执行事件的方法
2014/12/18 Javascript
深入学习JavaScript中的原型prototype
2015/08/13 Javascript
Javascript实现信息滚动效果
2017/05/18 Javascript
JS面向对象编程实现的Tab选项卡案例详解
2020/03/03 Javascript
[01:14]2014DOTA2展望TI 剑指西雅图newbee战队专访
2014/06/30 DOTA
python常见的格式化输出小结
2016/12/15 Python
Python编写登陆接口的方法
2017/07/10 Python
wxpython实现图书管理系统
2018/03/12 Python
python筛选出两个文件中重复行的方法
2018/05/31 Python
有关Python的22个编程技巧
2018/08/29 Python
Django实现网页分页功能
2019/10/31 Python
python 浅谈serial与stm32通信的编码问题
2019/12/18 Python
python 操作hive pyhs2方式
2019/12/21 Python
Pytorch对Himmelblau函数的优化详解
2020/02/29 Python
ASOS比利时:英国线上零售商及自有品牌
2018/07/29 全球购物
尼克松手表官网:Nixon手表
2019/03/17 全球购物
教育孩子心得体会
2014/01/01 职场文书
开展批评与自我批评发言材料
2014/05/15 职场文书
学校2014年度工作总结
2014/12/06 职场文书
北京颐和园导游词
2015/01/30 职场文书
2015年卫生监督工作总结
2015/05/21 职场文书
中学语文教学反思
2016/02/16 职场文书
mysql批量新增和存储的方法实例
2021/04/07 MySQL
修改MySQL的默认密码的四种小方法
2021/05/26 MySQL
Python用tkinter实现自定义记事本的方法详解
2022/03/31 Python
Redis keys命令的具体使用
2022/06/05 Redis
Android开发手册Chip监听及ChipGroup监听
2022/06/10 Java/Android