zoukankan      html  css  js  c++  java
  • Case1-用list写shoppingcart

    #define product list
    product_list = [
    ('Python book',45),
    ('iphone',5800),
    ('iwatch', 1500),
    ("bike",850),
    ("luckiin coffee",25)
    ]
    _shop =[]

    #input customer salary
    salary = input("Please input your Salary!》》》:")
    if salary.isdigit():
    _salary = int(salary)

    while True:
    for index,item in enumerate(product_list): # 输入正确数字salary,打印product list
    #print(product_list.index(item),item)
    print(index,item)
    print(" ")
    _choice = input("Please choice and Press 'q' to exit》》》:")

    if _choice.isdigit():
    _choice = int(_choice)

    if _choice <len(product_list ) and _choice>=0: #判读list范围
    p_item = product_list[_choice]
    if p_item[1]<=_salary: #钱够
    _shop.append(p_item) #添加记录到_shop 列表中
    _salary -= p_item[1] #扣钱
    print("Add %s in shopping cart! Your current balance is 33[31;1m%s33[0m" %(p_item, _salary))
    else:
    print("Not Enough Money!!!")
    else:
    print("商品 %s 不存在!!" %(_choice))

    elif _choice == 'q':
    print("------shopping list---------")
    for p in _shop:
    print (_shop)
    print("Exit!")
    exit()
    else:
    print("Invalid number!")
    break #注意break的位置

    else:
    print("Invalid Salary!!!")
    salary = input("Please input your Salary!》》》:")



    ###目前还有一个问题就是错误按键就退出,没有给用户重新input的机会!



    作为管理员的登录和增删改查
    #define the basic list
    product_list = [
    ('Python book',45),
    ('iphone',5800),
    ('iwatch', 1500),
    ("bike",850),
    ("luckiin coffee",25)
    ]
    action_list = [
    ("A","添加一条新记录"),
    ("D","删除一条记录"),
    ("U","更新商品价格"),
    ]
    #_admin_name = input("请输入管理员用户名》》》:")
    _admin_pwd = input("请输入密码》》》:")

    _count = 3
    while (_count <= 3) :
    if _admin_pwd == '1qaz':
    print("欢迎进入后台管理!",)

    print("-------------当前商品列表-------------")
    for index,item in enumerate(product_list):
    print(index,item)

    print(" ")
    print("*****商品列表操作快捷方式*****")
    #for act_item in (action_list):
    # print(act_item)
    print(action_list)

    print(" ")
    _action = input("请输入想要进行的操作》》》:")

    while _action != "q" :
    if _action == "A" :
    _item_name = input("请输入新的商品名称>>>:")
    _item_price = input("请输入商品价格》》》:")
    if _item_price.isdigit():
    _item_price = int(_item_price)
    if _item_price > 0:
    product_list.append([_item_name, _item_price])
    for index, item in enumerate(product_list):
    print(index, item)
    break
    else:
    print("不允许价格为负数!!!")
    else:
    print("输入的价格不是数字!")

    elif _action == "U":
    _item_no = input ("请输入商品编号》》》:")
    if _item_no.isdigit():
    _item_no = int(_item_no)
    if _item_no >= 0 and _item_no < len(product_list):
    _item_price = input("请输入商品的新的价格》》》:")
    if _item_price.isdigit():
    _item_price = int(_item_price)
    if _item_price > 0:
    _item_name = product_list[_item_no][0]
    print(_item_name)
    print(" ")
    del product_list[_item_no] ### udpate似乎不行,只能删一条,添一条记录了!???
    product_list.append([_item_name, _item_price])
    # product_list[_item_no][1] = _item_price ###?????
    for index, item in enumerate(product_list):
    print(index, item)
    break
    else:
    print("不允许价格为负数!!!")
    else:
    print("没有该商品编号")
    break
    else:
    print("商品标号必须为数字!")
    break
    else:
    print("输入了错误的快捷键!")
    _action = input("请输入想要进行的操作》》》:")

    break
    else:
    _admin_pwd = input ("错误的密码,你还有%s次机会,请重新输入》》》:"%(_count-1))
    _count = _count-1




  • 相关阅读:
    activemq的两种基本通信方式的使用及总结
    EJBCA认证系统结构及相关介绍
    白话经典算法系列之一 冒泡排序的三种实现 【转】
    springcloud配置需要主要的地方
    Spring的RestTemplate
    mysql中的事务
    list去重
    webpack的使用
    rocketmq在linux搭建双master遇到的坑
    redis配置文件详解
  • 原文地址:https://www.cnblogs.com/ywyin/p/8989640.html
Copyright © 2011-2022 走看看