• python 列表应用-简单的购物车


    # -*- coding=utf-8 -*-
    # Author:gx
    #购物车

    product_list = [
    ("Iphone",5800),
    ("Mac Pro",10800),
    ("Bike",800),
    ("Watch",12000),
    ("Alex Python",50),
    ("Coffee",30)
    ]

    shopping_list = []

    salary = input("Input your salary:") #输入工资
    if salary.isdigit(): #判断输入的工资是不是数字
    salary = int(salary) #是数字转成int型
    while True:
    for item in product_list :
    print(product_list.index(item),item) #打印出商品下标及商品名称及价格
    use_choice = input("请选择你要买的商品编号--->:")
    if use_choice.isdigit(): #判断输入的是否数字
    use_choice = int(use_choice) #转int
    if use_choice < len(product_list) and use_choice >= 0: #判断输入的商品编号是否在范围内
    p_item = product_list[use_choice]
    if p_item[1] <= salary: #买的起
    shopping_list.append(p_item) #加入购物车列表
    salary = salary - p_item[1] #计算余额
    print("Added %s into shopping cart,your current balance is 33[31;1m%s33[0m" %(p_item,salary)) #打印余额
    else: #买不起
    print("33[41;1m你的余额仅剩[%s]33[0m" % salary) #打印余额
    else: #不在已有的商品列表中
    print("product code %s is not exist",use_choice) #提示不存在
    elif use_choice =='q': #判断输入q退出
    print("------shopping list--------")
    for p in shopping_list:
    print(p) #循环打印出购买的商品列表
    print("your current balance:",salary) #打印余额
    exit() #退出
    else: #判读输入的既不是数字,也不是q
    print("invalid option")
    else:
    print("please input int salary ")
  • 相关阅读:
    新博客
    【Gym-100712 #H】Bridges
    【CodeForces817F】MEX Queries
    【POJ1734】Sightseeing trip
    【Aizu2968】Non-trivial Common Divisor
    【Gym-101473 #I】Patches
    【POJ2228】Naptime
    【CodeForces219D】Choosing Capital for Treeland
    【URAL1018】Binary Apple Tree
    深入探索C++对象模型(五)
  • 原文地址:https://www.cnblogs.com/axiangstudy/p/13623767.html
Copyright © 2020-2023  润新知