python实现购物车功能

 更新时间:2022年02月09日 09:23:46   作者:乱弹世界  
这篇文章主要为大家详细介绍了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"

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

运行结果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python访问PostgreSQL数据库详细操作

    Python访问PostgreSQL数据库详细操作

    postgresql是常用的关系型数据库,并且postgresql目前还保持着全部开源的状态,这篇文章主要给大家介绍了关于Python访问PostgreSQL数据库的相关资料,需要的朋友可以参考下
    2023-11-11
  • pandas:get_dummies()与pd.factorize()的用法及区别说明

    pandas:get_dummies()与pd.factorize()的用法及区别说明

    这篇文章主要介绍了pandas:get_dummies()与pd.factorize()的用法及区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Python常用小技巧总结

    Python常用小技巧总结

    这篇文章主要介绍了Python常用小技巧,实例总结了Python关于字典、字符串、随机数等操作技巧,非常简单实用,需要的朋友可以参考下
    2015-06-06
  • Python中使用dwebsocket实现后端数据实时刷新

    Python中使用dwebsocket实现后端数据实时刷新

    dwebsocket是Python中一款用于实现WebSocket协议的库,可用于后端数据实时刷新。在Django中结合使用dwebsocket和Channels,可以实现前后端的实时通信,支持双向数据传输和消息推送,适用于实时聊天、数据监控、在线游戏等场景
    2023-04-04
  • TensorFlow人工智能学习Keras高层接口应用示例

    TensorFlow人工智能学习Keras高层接口应用示例

    这篇文章主要为大家介绍了TensorFlow人工智能学习中Keras高层接口的应用示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-11-11
  • 2020新版本pycharm+anaconda+opencv+pyqt环境配置学习笔记,亲测可用

    2020新版本pycharm+anaconda+opencv+pyqt环境配置学习笔记,亲测可用

    这篇文章主要介绍了2020新版本pycharm+anaconda+opencv+pyqt环境配置学习笔记,亲测可用,特此分享到脚本之家平台,需要的朋友可以参考下
    2020-03-03
  • HTTPX入门使用教程

    HTTPX入门使用教程

    HTTPX是一款Python栈HTTP客户端库,它提供了比标准库更高级别、更先进的功能,如连接重用、连接池、超时控制、自动繁衍请求,下面通过本文介绍HTTPX入门知识和基本用法,感兴趣的朋友一起看看吧
    2023-12-12
  • python线程中的同步问题及解决方法

    python线程中的同步问题及解决方法

    这篇文章主要介绍了python线程中的同步问题及解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • python标准库OS模块详解

    python标准库OS模块详解

    这篇文章主要介绍了python标准库OS模块详细介绍,需要的朋友可以参考下
    2020-03-03
  • python多进程使用函数封装实例

    python多进程使用函数封装实例

    这篇文章主要介绍了python多进程使用函数封装实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05

最新评论