pyrhon 第一个小购物车例子
product_list=[["iphone",6000],["苹果电脑",12000],["衣服",1000],["wine",500]] shopping_list=[] salary = input("请输入你的工资:") 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_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 balance is \033[31;1m%s\033[0m元" %(p_item,salary)) else: print("\033[34;1m你的余额只剩[%s],无法购买此商品!\033[0m" %(salary)) else: print("\033[34;1m你输入的商品编号不存在,请重新输入:\033[0m") #编号输出超过len(product_list)长度,提示错误 elif user_choice == 'q': print('--------shopping_list----------') for p in shopping_list: print(p) print("已经购买的商品有:",shopping_list[:]) print("your current balance is \033[31;1m%s\033[0m元" %(salary)) exit() else: print("invalid option")
小白初学,还有很多不会的,或者不对的,请勿喷!