python实现购物车功能

作者:乱弹世界 时间:2021-06-02 08:08:12 

本文实例为大家分享了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"

功能挺简单,就是涉及到列表的增加和删除,还有一些逻辑的判断处理。

运行结果如下:

python实现购物车功能

来源:https://blog.csdn.net/linxi7/article/details/70582273

标签:python,购物车
0
投稿

猜你喜欢

  • 在Windows的Apache服务器上配置对PHP和CGI的支持

    2023-10-20 22:12:17
  • 随Linux开机自动启动mysql

    2009-12-29 10:14:00
  • python语言元素知识点详解

    2023-07-30 03:33:08
  • 解析smarty模板中类似for的功能实现

    2023-11-15 12:53:40
  • 如何创建并使用一个断开连接的记录集的数据访问页?

    2009-11-14 20:50:00
  • Python 安装setuptools和pip工具操作方法(必看)

    2023-11-06 11:46:07
  • Python正规则表达式学习指南

    2021-04-11 15:21:16
  • Yahoo发布一款FireFox网站开发插件

    2007-09-23 16:11:00
  • 用Python在Excel里画出蒙娜丽莎的方法示例

    2023-12-18 02:59:21
  • 从网页设计开始

    2008-06-30 12:17:00
  • 修改Linux下MySQL 5.0的默认连接数

    2009-09-01 10:16:00
  • 互联网一家之言(一):叫用户为你买单

    2009-06-09 11:32:00
  • 适合所有表的添加、删除、修改的函数

    2008-04-15 15:29:00
  • 网站导航设计的6大分类

    2010-07-12 18:46:00
  • Python利用随机函数生成变化图形详解

    2021-07-02 06:54:54
  • python使用socket远程连接错误处理方法

    2023-05-13 04:56:48
  • 用途相似的标签

    2008-05-23 13:11:00
  • python 捕获 shell/bash 脚本的输出结果实例

    2023-10-04 15:27:41
  • 发一新浪招聘的图片滚动控制JS效果

    2011-08-10 19:17:25
  • ExtJS 开发总结

    2009-04-28 13:05:00
  • asp之家 网络编程 m.aspxhome.com