需求: 1.启动程序后,让用户输入工资,然后打印商品列表; 2.允许用户根据商品编号购买商品; 3.用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒; 4.可随时退出,退出时打印已购买的商品列表。 # Author: Ewan Wang shopping_list = [] goods_list = [ ('Nike shoes',800), ('Mjstyle trousers',500), ('Bag',300), ('Dictionary',100), ('Cup',80), ('Skin lotion',60) ] salary = input('Input your salary:') if salary.isdigit(): salary = int(salary) while True: for index,item in enumerate(goods_list): #print(goods_list.index(item),item) print(index,item) user_choice = input('The product you want to buy is') if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(goods_list) and user_choice >=0: p_item = goods_list[user_choice] if salary >= p_item[1]: shopping_list.append(p_item) salary -= p_item[1] print('Add %s in your shopping cart,your salary balance is %s'%(p_item[0],salary)) else: print('Your balance is insufficient.') else: print('Invalid option.') elif user_choice == 'q':# 这里是==而非= print('----------shopping list---------') for p in shopping_list: print(p) exit()