首页 > 代码库 > 编写购物车,初级版。。。

编写购物车,初级版。。。

product_list=[
    (‘iphone‘,5000),
    (‘mac‘,8000),
    (‘telsa‘,9000),
    (‘mac pro‘,4300),
    (‘iwatch‘,8434),
]
shopping_list=[]
salary=input(‘pls input your salary:‘)
if salary.isdigit():
    salary=int(salary)
    while True:
        for index,item in enumerate(product_list):
            print(index,item)
        user_choice=input(‘请选择要买的商品:‘)
        # if user_choice<product_list.index(item)
        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 cat,you salary %s‘ %(p_item,salary))
                else:
                    print(‘你买不起该商品,余额不足‘)
            else:
                print(‘你输入的商品%s不存在,请重新输入。。。‘ %user_choice)

        elif user_choice==‘q‘:

            print(‘shopping list‘)
            for p in shopping_list:
                print(p)
            print(‘你现在的余额为%s‘%salary)
            exit()
        else:
            print(‘invalid option‘)

  

编写购物车,初级版。。。