美文网首页
4.1、购物车系统

4.1、购物车系统

作者: Yerban | 来源:发表于2018-10-16 17:58 被阅读0次

购物车系统

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
  4. 可随时退出,退出时,打印已购买商品和余额
#!/usr/bin/env python
# coding: utf-8
# Author: Yerban

product_list = [
    ('iphone', 5800),
    ('macbook', 14000),
    ('bike', 800),
    ('iwatch', 10600),
    ('coffee', 31),
    ('alex python', 120)
]

shopping_list = []
salary = input("input your salary:")

# isdigit() 方法检测字符串是否只由数字组成。
if salary.isdigit():
    salary = int(salary)
    while True:
# enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
# enumerate() 方法的语法:enumerate(sequence, [start=0])
# sequence -- 一个序列、迭代器或其他支持迭代对象。
# start -- 下标起始位置。enumerate(product_list, start=3)
        for index, item in enumerate(product_list):
            print(index, item)
# 笨方法打印出列表索引,先遍历出原list的元素,然后再用index打印出下标。
#        for item in product_list:
#            print(product_list.index(item), item)

        user_choice = input("Input purchase item? >>>:")

        if user_choice.isdigit():
            user_choice = int(user_choice)
            if len(product_list) > user_choice >= 0:
                p_item = product_list[user_choice]
                if p_item[1] <= salary:  # 买得起
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    # 31是字体标红,41是字体背景标红
                    print("Added %s into shopping cart, your current balance is \033[31;1m%s\033[0m ." % (p_item, salary))
                else:
                    print("\033[41;1m你的余额剩余[%s],还买个毛线啊...\033[0m ." % salary)
            else:
                print("product code [%s] is not exist!" % user_choice)
        elif user_choice == "q":
            print("------shopping list------")
            for p in shopping_list:
                print(p)
            print("Your current balance :", salary)
            exit()

else:
    print("Input is wrong!")

相关文章

  • 4.1、购物车系统

    购物车系统 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是...

  • 后端存储3

    购物车系统的主要功能包括:加购、购物车列表页和结算下单。 购物车系统需要保存两类购物车,一类是未登录情况下的“暂存...

  • Jenkins+SVN 持续集成环境搭建

    4.1 系统结构总述 创建虚拟机安装 Linux 系统 版本控制子系统 -----------Subversion...

  • 给予消息队列实现分布式事务

    给予消息队列实现分布式事务 场景: 订单系统产生订单,购物车系统减购物车中的商。 实现思路 : 订单系统在消息队列...

  • Android源码学习目录

    Android系统架构 Android源码目录 Android系统的启动过程 Android init进程4.1 ...

  • 2017年10月21日(周六)

    《大数据系统基础》3 文件存储《大数据系统基础》4 处理框架 4.1-4.3

  • ElsaWin

    4.1版本 5.0版本 XP系统:SQL 2008

  • 饿了吗开源组件库Element模拟购物车系统

    传统的用html+jquery来实现购物车系统要非常的复杂,但是购物车系统完全是一个数据驱动的系统,因此采用诸如V...

  • 2018-09-26

    9月25日任务 4.1 df命令 4.2 du命令 4.3/4.4 磁盘分区 4.1 df命令 汇报文件系统的磁盘...

  • docker 安装sentry

    docker 安装lsb_release -a 查看系统LSB Version: :core-4.1-am...

网友评论

      本文标题:4.1、购物车系统

      本文链接:https://www.haomeiwen.com/subject/hpahzftx.html