首页 > 代码库 > PYTHON 购物车程序

PYTHON 购物车程序

product_list = [
    (iphone,50000),
    (Mac Pro,9900),
    (Bike,8000),
    (Watch,160000),
    (Coffee,600),
    (‘lqgyc,1200)

]
shopping_list = []
salary = input("请输入工资:")
if salary.isdigit():  # isdigit() 方法检测字符串是否只由数字组成
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            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 balar:",salary)

                else:
                    print("你的余额只剩[%s]啦,无法购买"% salary)
            else:
                print("product code [%s] is not exist !" % user_choice)
        elif user_choice == q:
            print(---商品列表!---)
            for p in shopping_list:
                print(p)
            print("your current balance:", salary)
            exit()
        else:
            print("请输入商品序列号!")

 

PYTHON 购物车程序