购物车一点

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
  4. 可随时退出,退出时,打印已购买商品和余额

代码如下:

# author = "zhuyouen"

product = [
    ('Iphone',5800),
    ('Mac book',10800),
    ('Bike',2800),
    ('Watch',3000),
    ('cloths',1000),
    ('book',500),
]

shopping_list=[]
salary = input('Your salary is:')
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product):
            print(index,item)
        user_choice = input("请输入商品的>>>")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product) and user_choice >=0:
                p_item = product[user_choice]
                if p_item[1] <= salary:
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print('Add %s in your shopping list,your balance is \033[31;1m%s\033[0m' %(shopping_list,salary))
                else:
                    print('\033[41;1mYour balbnace  %s is not enough.\033[0m' %(salary))
            else:
                print('Your choice %s is exceed' %(user_choice))
        elif user_choice == 'q':
            print('----shopping list----')
            for _i in shopping_list:
                print(_i)
            exit()
        else:
            print('Your input %s invaid option' % (user_choice))
else:
    print('Your input %s invaid option' % (salary))



D:\python35\python.exe D:/PycharmProjects/zhuyouen/0406/task_1_3.py
Your salary is:11111
0 ('Iphone', 5800)
1 ('Mac book', 10800)
2 ('Bike', 2800)
3 ('Watch', 3000)
4 ('cloths', 1000)
5 ('book', 500)
请输入商品的>>>1
Add [('Mac book', 10800)] in your shopping list,your balance is 311
0 ('Iphone', 5800)
1 ('Mac book', 10800)
2 ('Bike', 2800)
3 ('Watch', 3000)
4 ('cloths', 1000)
5 ('book', 500)
请输入商品的>>>5
Your balbnace  311 is not enough.
0 ('Iphone', 5800)
1 ('Mac book', 10800)
2 ('Bike', 2800)
3 ('Watch', 3000)
4 ('cloths', 1000)
5 ('book', 500)
请输入商品的>>>q
----shopping list----
('Mac book', 10800)
posted @ 2017-04-07 16:09  三个字  阅读(133)  评论(0编辑  收藏  举报