Python实现购物车功能的方法分析


Posted in Python onNovember 10, 2017

本文实例讲述了Python实现购物车功能的方法。分享给大家供大家参考,具体如下:

1、程序的源代码如下:

salary = input('input your salary:')
if salary.isdigit:
  salary = int(salary)
else:
  exit('salary is not digit!!')
welcome_msg = 'welcome to our shoping mall'
print(welcome_msg.center(50,'-'))
product_list = [
  ('Iphone',5888),
  ('Mac Air',8000),
  ('XiaoMi',19.9),
  ('coffee',30),
  ('Tesla',820000),
  ('Bike',700),
  ('Cloth',200)
]
shop_car = []
#推出标志位
exit_flag = 0
while exit_flag is not True:
  print('product list :'.center(50,'-'))
  for item in enumerate(product_list):
    index = item[0]   #获得商品序号
    p_name = item[1][0]  #获得商品名称
    p_price= item[1][1]  #获得商品价格
    print(index,p_name,p_price)
  user_choice = input('[q=quit,c=check] what do you want to buy?:')
  if user_choice.isdigit():
    user_choice = int(user_choice)
    if user_choice < len(product_list): #输入的商品序号要在范围之内
      p_item = product_list[user_choice] #选择的商品
      if p_item[1] <= salary: #买得起
        shop_car.append(p_item) #放入购物车
        salary -= p_item[1] #减钱
        print('purchased products are:'.center(40, '-'))
        for item in shop_car:
          print(item)
        print('Your balance is [%s]' % salary)
      else:          #买不起
        print('Your balance is [%s],cannot buy the product!'%salary)
    else:
      print('we do not have the product,please input again!')
  elif user_choice == 'q' or user_choice == 'quit':
    print('purchased products are:'.center(40,'-'))
    for item in shop_car:
      print(item)
    print('END '.center(40,'-'))
    print('Your balance is [%s]'%salary)
    exit_flag =True
  elif user_choice == 'c' or user_choice == 'check':
    print('purchased products are:'.center(40, '-'))
    for item in shop_car:
      print(item)
    print('Your balance is %d' % salary)
    print('Check '.center(40, '-'))
  else:
    print('Incorrect input ,please input again!!')

2、程序运行结果

input your salary:10000
-----------welcome to our shoping mall------------
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:0
added [[('Iphone', 5888)]] into shop car ,yourcurrent balence is 4112
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:1
Your balance is [4112],cannot buy the product!
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:c
shop car list are [[('Iphone', 5888)]],your balance is 4112
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:4
Your balance is [4112],cannot buy the product!
------------------product list :------------------
0 Iphone 5888
1 Mac Air 8000
2 XiaoMi 19.9
3 coffee 30
4 Tesla 820000
5 Bike 700
6 Cloth 200
[q=quit,c=check] what do you want to buy?:q
--------purchased products are:---------
('Iphone', 5888)
------------------END ------------------
Your balance is [4112]
Process finished with exit code 0

3、学习到的知识点

(1)print('product list :'.center(50,'-'))

>>> print('product list :'.center(50,'-'))
------------------product list :------------------

(2)for item in enumerate(product_list):

>>> for item in enumerate(product_list):
print(item)
(0, ('Iphone', 5888))
(1, ('Mac Air', 8000))
(2, ('XiaoMi', 19.9))
(3, ('coffee', 30))
(4, ('Tesla', 820000))
(5, ('Bike', 700))
(6, ('Cloth', 200))

enumerate函数返回一个生成器对象:(index,item)的元组。

(3)这里使用了列表+元组的形式存储商品列表,而不是字典,因为字典是无序的,每次打印顺序都不一样,而且不能通过索引进行取值。

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python使用urllib2获取网络资源实例讲解
Dec 02 Python
python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法
May 15 Python
浅谈Python基础之I/O模型
May 11 Python
Python实现感知器模型、两层神经网络
Dec 19 Python
Python中协程用法代码详解
Feb 10 Python
python 实现从高分辨图像上抠取图像块
Jan 02 Python
Tensorflow的常用矩阵生成方式
Jan 04 Python
使用python无账号无限制获取企查查信息的实例代码
Apr 17 Python
Python数组拼接np.concatenate实现过程
Apr 18 Python
python获取响应某个字段值的3种实现方法
Apr 30 Python
python中pathlib模块的基本用法与总结
Aug 17 Python
Python 利用argparse模块实现脚本命令行参数解析
Dec 28 Python
Python实现的单向循环链表功能示例
Nov 10 #Python
Python3中的列表,元组,字典,字符串相关知识小结
Nov 10 #Python
浅谈Python处理PDF的方法
Nov 10 #Python
django开发教程之利用缓存文件进行页面缓存的方法
Nov 10 #Python
python使用邻接矩阵构造图代码示例
Nov 10 #Python
python先序遍历二叉树问题
Nov 10 #Python
简单了解OpenCV是个什么东西
Nov 10 #Python
You might like
提升PHP速度全攻略
2006/10/09 PHP
php5.2.0内存管理改进
2007/01/22 PHP
apache rewrite_module模块使用教程
2008/01/10 PHP
解决cPanel无法安装php5.2.17
2014/06/22 PHP
php从字符串创建函数的方法
2015/03/16 PHP
thinkphp5 migrate数据库迁移工具
2018/02/20 PHP
Yii2框架加载css和js文件的方法分析
2019/05/25 PHP
关于二级域名下使用一级域名下的COOKIE的问题
2011/11/07 Javascript
Javascript添加监听与删除监听用法详解
2014/12/19 Javascript
jQuery仿gmail实现fixed布局的方法
2015/05/27 Javascript
JQuery标签页效果实例详解
2015/12/24 Javascript
深入理解jQuery 事件处理
2016/06/14 Javascript
Bootstrap 3 进度条的实现
2017/02/22 Javascript
基于JavaScript实现每日签到打卡轨迹功能
2018/11/29 Javascript
JavaScript中import用法总结
2019/01/20 Javascript
JavaScript函数式编程(Functional Programming)高阶函数(Higher order functions)用法分析
2019/05/22 Javascript
js实现贪吃蛇小游戏
2019/10/29 Javascript
flask入门之文件上传与邮件发送示例
2018/07/18 Python
python得到windows自启动列表的方法
2018/10/14 Python
利用anaconda作为python的依赖库管理方法
2019/08/13 Python
Python中断多重循环的思路总结
2019/10/04 Python
Python concurrent.futures模块使用实例
2019/12/24 Python
tensorflow-gpu安装的常见问题及解决方案
2020/01/20 Python
python中把元组转换为namedtuple方法
2020/12/09 Python
canvas压缩图片以及卡片制作的方法示例
2018/12/04 HTML / CSS
英国天然保健品网站:Simply Supplements
2017/03/22 全球购物
windeln官方海外旗舰店:德淘超人气母婴超市
2017/12/15 全球购物
如果NULL和0作为空指针常数是等价的,那我到底该用哪一个
2014/09/16 面试题
创建索引时需要注意的事项
2013/05/13 面试题
材料加工工程求职信
2014/02/19 职场文书
学习方法演讲稿
2014/05/10 职场文书
初中学校对照检查材料
2014/08/19 职场文书
2014年文艺部工作总结
2014/11/17 职场文书
团队合作精神学习心得体会
2016/01/19 职场文书
Python 恐龙跑跑小游戏实现流程
2022/02/15 Python
MySQL学习之基础命令实操总结
2022/03/19 MySQL