zoukankan      html  css  js  c++  java
  • Python学习4:商城购物

    商城购物

    products = [["iPhone Pro",12888],["MacBook Pro",22999],["HuaWei P40 Pro",6999],["Mi 10 Pro",5999],["Mi Game Book",8999]]
    shop_car = []  # 用户购物车
    shop_cost = 0  #用户花费的金额
    exit_log = False  #标志位,默认设为False,退出
    while not exit_log:
        print("----------商品列表----------")
        for index,i in enumerate(products):
            print("%s %s %s"%(index,i[0],i[1]))
        user_choice = input("
    输入你想购买的产品序号(按“q”退出):")
        if user_choice.isdigit():
            #判断用户输入的是否是数字
           user_choice = int(user_choice)  #强制转换为数字
           if user_choice>=0 and user_choice < len(products):
              #  判断用户购买的商品是否在商品列表中
              shop_car.append(products[user_choice])  # 加入购物车
              shop_cost += products[user_choice][1]  # 计算费用
              print("
     %s 已经加入你的购物车
    "%products[user_choice])
           else:
               print("抱歉,此商品不存在
    ")
        elif user_choice == "q":
            # 用户选择退出
           if len(shop_car)>0:
               # 判断用户是否购买了商品
               print("
    ----------你的购物车----------")
               for index,i in enumerate(shop_car):
                   # index和i为临时变量,与前一个for循环里index和i作用的列表不同,可重复使用
                    print("%s %s"%(i[0],i[1]))
               print("
    你此次购物的花费合计是:%s元
    "%shop_cost)
               exit_log = True  # 退出购物
           else:
               exit_log = True  # 未购买商品,不打印购物车商品,直接退出
        else:
            # 输入不合法
            exit_log = True
    
  • 相关阅读:
    mysql 5.7 安装手册(for linux)
    Git服务器分类
    Git服务器安装详解及安装遇到问题解决方案
    使用git进行版本管理
    Git 忽略一些文件不加入版本控制
    Windows下搭建基于SSH的Git服务器
    linux系统下mysql跳过密码验证登录和创建新用户
    阅读《不止代码》之心得分享
    Sonar安装和常见问题解决
    Eclipse安装Sonarlint插件
  • 原文地址:https://www.cnblogs.com/yanyanqaq/p/13051157.html
Copyright © 2011-2022 走看看