python实现购物车功能
作者:乱弹世界 发布时间:2021-06-02 08:08:12
标签:python,购物车
本文实例为大家分享了python实现购物车功能的具体代码,供大家参考,具体内容如下
功能要求:
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车
代码:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
shopping_list = [
("Iphone", 5000),
("Delicious food", 48),
("Mac book", 9800),
("Huawei", 4800),
("Alex python", 32),
("coffee", 24)
]
shopping_cart = []
salary = raw_input('please input salary: ')
if not salary.isdigit():
print "salary must be digit,run again"
exit()
else:
salary = int(salary)
while True:
print "------products list is--------"
for index, item in enumerate(shopping_list):
print "\033[32m%s, %s\033[0m" %(index, item)
choice = raw_input('please input choice[q(uit)]>>> ')
if choice.isdigit():
choice = int(choice)
if choice < len(shopping_list) and choice >= 0:
product = shopping_list[choice]
if salary > product[1]:
confirm = raw_input('do you want to buy now[y/n]: ')
if confirm == 'y':
shopping_cart.append(product)
salary -= product[1]
print "you bought %s,price is %d, your balance is %d" % (product[0], product[1], salary)
else:
print 'select again'
else:
add_confirm = raw_input("your balance is: %d, not enough, do you want to add more?[y/n]" % salary)
if add_confirm == 'y':
add_salary = raw_input('add the money: ')
if add_salary.isdigit():
add_salary = int(add_salary)
salary += add_salary
print "now balance is %d: " % salary
else:
print "the money must be digit."
else:
print "------shopping cart list---------: "
for index, item in enumerate(shopping_cart):
print index, item
else:
print "choice must be 0~5."
elif choice == 'q':
remove_product = raw_input("do you want remove product or exits now [y/n] ")
if remove_product == "y":
print "-----------your shopping cart lists-------------: "
for index, item in enumerate(shopping_cart):
print index, item
remove_choice = raw_input('please input your remove choice>>> ')
if remove_choice.isdigit() and int(remove_choice) < len(shopping_cart) and int(remove_choice) >= 0:
salary += shopping_cart[int(remove_choice)][1]
del shopping_cart[int(remove_choice)]
print "-----------new shopping cart lists-------------: "
for index, item in enumerate(shopping_cart):
print index, item
print "your balance is %d" % salary
else:
print "input error, again"
else:
print "exit now"
exit()
else:
print "-----------shopping cart lists-------------: "
for index, item in enumerate(shopping_cart):
print index, item
print "\033[31mchoice must be digit,exit\033[0m"
功能挺简单,就是涉及到列表的增加和删除,还有一些逻辑的判断处理。
运行结果如下:
来源:https://blog.csdn.net/linxi7/article/details/70582273


猜你喜欢
- computedcomputed只接收一个getter函数1、getter必须有返回值2、computed返回一个只读响应式ref对象 (只
- 在腾讯的微信公众平台开发者文档,网页授权获取用户基本信息这一节中写道”在微信公众号请求用户网页授权之前,开发者需要先到公众平台网站的我的服务
- 实际项目中会涉及到需要对有些函数的响应时间做一些限制,如果超时就退出函数的执行,停止等待。可以利用python中的装饰器实现对函数执行时间的
- TCP客户端程序开发1. 开发 TCP 客户端程序开发步骤回顾创建客户端套接字对象和服务端套接字建立连接发送数据接收数据关闭客户端套接字2.
- 本节重点掌握Cpython的GIL解释器锁的工作机制掌握GIL与互斥锁掌握Cpython下多线程与多进程各自的应用场景本节时长需控制在45分
- 介绍psutil能够轻松实现获取系统运行的进程和系统利用率。导入模块import psutils获取系统性能信息CPU信息使用cpu_tim
- 你是一位交互设计师吗?告诉我,你具体做些什么?我是做网站设计的?听起来不够专业。我是做网页设计的,听起来……你们是做界面的……恩,好吧,我勉
- Python2.6 之前:字符串转换为整形和浮点型>>>import string>>>string.a
- 问题描述:使用 SQL 2005 w/ SP2 的汇出汇入精灵将数据从 Access 汇入到 SQL2005 发生了错误,但使用在SQL 2
- 使用opencv自带的模板匹配1、目标匹配函数:cv2.matchTemplate()res=cv2.matchTemplate(image
- 今天有朋友问我 Turtle的安装问题,在这里简单说在github上我们可以看到Turtle这个库很久没有更新了,里面还有一些python3
- 数据库表表面上存在索引和防错机制,然而一个简单的查询就会耗费很长时间。Web应用程序或许在开发环境中运行良好,但在产品环境中表现同样糟糕。如
- <%'解析一个xml文件的公用函数集合dim document'装载一个xml文档,函数名Loaddocument(文
- 正在看的ORACLE教程是:Access2000迁移到Oracle9i要点。 &nb
- 今天仔细研究了下GD的一些相关技术,顺手也研究下GD中文乱码的问题。 使用GD库输出中文字符串,调用imagestring是没有
- 窗口函数OVER (PARTITION BY xxx ORDER BY xxx ASC/DESC)测试数据表及数据测试表 employeeC
- 解决此问题的几个关键点如下:1、该现象只会出现在NTFS文件系统中。2、由NTFS文件系统的访问权限导致。 一般手工操作的解决方案
- python 中datetime中strptime用法,具体代码如下所示:import datetimeday20 = datetime.d
- 本文实例讲述了golang实现unicode转换为字符串string的方法。分享给大家供大家参考,具体如下:package mainimpo
- 如何在Typescript中使用for...in ?本人在TS中用for...in出现了些问题,也想到了一些解决方法。那么先来看看下面报错的