Python3中实现简单的购物车程序

product_list = [
    ('iphone',5800),
    ('imac',15800),
    ('watch',9800),
    ('cloth',550),
    ('coffe latee',35),
    ('body call',200),
]
shopping_list = []
salary = input('please input your salary:').strip()
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            print(index,item)
        user_choice = input('please input your choice'.center(50,'*')).strip()
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if 0 <= user_choice < len(product_list):
                buy_item = product_list[user_choice]
                if salary >= buy_item[1]:
                    shopping_list.append(buy_item)
                    salary -= buy_item[1]
                    print('you have put the \033[32;1m"%s" \033[0min your cast,your balance is\033[31;1m %d\033[0m'%(buy_item[0],salary))
                else:
                    print('\033[41;1mfuck off!!! you can`t afford it!!! your balance is %s !!!\033[0m'%(salary))
            else:
                print('product code [%s] is not exist!!!'%user_choice)
        elif user_choice == 'q' or user_choice == 'quit':
            print('you have buyed:'.center(50,'-'))
            print(shopping_list)
            exit()
        else:
            print('Invalid choice!!!')

  

posted @ 2017-03-01 17:54  KaShing  阅读(266)  评论(0编辑  收藏  举报