• python 3.6练习题(仿购物车)


    opop = [
    ('Iphone', 9800),
    ('Bike', 800),
    ('Mac Pro', 12000),         #定义商品列表
    ('Pyhon book', 120),
    ('Telas', 429800),
    ('Memory', 7000),
    ('hard', 4000),
    ('Upan', 90),
    ]
    shopping_list = []
    salary = input("请输入你的工资: ")          #输入工资
    if salary.isdigit():                  #判断输入的是不是数字
      salary = int(salary)              #将工资转换为整形
      while True:                 #死循环入口
        for index, item in enumerate(opop):          #打印商品列表,同时把下标也打印出来
          print(index, item)
        user_choice = input("---> 请输入你要买的商品编号: ") #让用户选择买什么商品
        if user_choice.isdigit():                 #判断用户输入是不是数字类型
          user_choice = int(user_choice)                 #将用户输入的更改为整形
          if user_choice < len(opop) and user_choice >= 0:   #判断用户的输入是否大于列表的长度
            p_item = opop[user_choice]          #通过下标把商品取出来
            if p_item[1] <= salary:             #如果该商品价格小于当前的工资,表示买的起
              shopping_list.append(p_item)       #把取来的商品添加到已购买列表中
              salary -= p_item[1]                 #从工资中自动扣钱,并打印出已买商品和工资所剩数目
              print(" %s 已添加到购物车,您目前的余额还剩 33[31;1m%s33[0m 元 " % (p_item, salary))
            else:                     #买不起则执行该句
              print("你的余额只剩 33[41;1m%s33[0m 元已不足购买该商品 " % salary)
          else:
            print("---> 该商品不存在 <---")
            break
        elif user_choice == 'q':
          print('-'*20, "已购买清单", '-'*20)
          for i in shopping_list:
            print(i)
          print("您的当前余额还剩: ", salary)
          exit()
        else:
          print("invalid option")

    ----------------------------------- 片段解释分割线 -----------------------------------

    while True下面的enumerate语法示例:

    a = [1,2,3]
    for i in enumerate(a):print(i)
    输出结果:(把列表的下标都打印出来)
    (0, 1)
    (1, 2)
    (2, 3)

  • 相关阅读:
    let 和 const 命令
    python连接oracle
    Python中小整数对象池和大整数对象池
    前端内容流程导图
    bootstrap插件的一些常用属性介绍
    bootstrap的引入和使用
    Linux 重定向
    Mongodb 备份 数据导出导入
    Mongodb 副本集
    Redis 模糊查询删除操作
  • 原文地址:https://www.cnblogs.com/smlile-you-me/p/8888100.html
Copyright © 2020-2023  润新知