• 购物车的实例


    2020-02-17
    _author_ = "chen"
    product_list = [
    ('iphone', 5800),
    ('mac book', 9800),
    ('bike', 800),
    ('watch', 10600),
    ('coffee', 31),
    ('python', 120),

    ]
    shopping_list = []
    salary = input("input your salary:")
    if salary.isdigit():
    salary = int(salary)
    while True:
    for index, item in enumerate(product_list): # enumerate取下标
    print(index, item)
    user_choice = input("选择要买什么?:")
    if user_choice.isdigit(): # 判别是不是数字
    user_choice = int(user_choice)
    if user_choice < len(product_list) and user_choice >= 0:
    p_item = product_list[user_choice]
    if p_item[1] <= salary: # 买得起
    shopping_list.append(p_item)
    salary -= p_item[1]
    print("Added %s into shopping cart ,your current balance is33[31:1m %s33[0m" % (
    p_item, salary)) # 打印颜色
    else:
    print(":33[41;1m你的余额只剩[%s]啦,买毛线啊33[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)
    break
    else:
    print("invalid option")
  • 相关阅读:
    Oracle的建表约束
    Sql的增删改操作
    关联查询之92语法和99语法
    日常编程练习(三)
    日常编程练习(二)
    日常编程练习(一)
    C++ 赋值运算符函数
    内存管理
    进程同步——经典的同步问题
    I/O 阻塞与非阻塞,同步与异步
  • 原文地址:https://www.cnblogs.com/cy2268540857/p/12322690.html
Copyright © 2020-2023  润新知