python基础入门---购物车程序练习实例

product_list = [('shoe',1299),
            ('book',68),
            ('pad',2999),
            ('char',249),
            ('switch',1499),
            ('iwatch',3270),
            ]
shopping_list =[]
salary = input("input you salary")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            #print(product_list.index(item),item)
            print(index,item)
        user_chose = input("select what do you want to buy:" )
        if user_chose.isdigit():
            user_chose = int(user_chose)
            if user_chose < len(product_list) and user_chose >=0:
                p_item = product_list[user_chose]
                if p_item[1] <= salary:#买得起
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("Added %s into shopping cart,your balance is \033[31;1m%s\033[0m"%(p_item,salary))
                else:
                    print("\033[41;1m you balance has been only [%s],you can't buy anything \033[0m"%salary)
            else:
                print("product_code [%s] is not exit!" %user_chose)
        elif user_chose =='q':
            print("------shopping list-------")
            for p in shopping_list:
                print(p)
            print("you current balance:",salary)
            exit()
        else:
            print("invalid select")

猜你喜欢

转载自www.cnblogs.com/qjhh/p/12347516.html