# Author:Eric Zhao# -*- coding:utf-8 -*-‘‘‘需求:启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额‘‘‘ product_list = [ (‘IPhone‘,5000), (‘Bike‘, 500), (‘Car‘, 50000), (‘Hat‘, 50)]shopping_list = []salary = input(‘Please input your salary..‘)if salary.isdigit(): salary = int(salary) while True: # 死循环 # for item in product_list: # print(product_list.index(item),item) for index,item in enumerate(product_list): print(index,item) user_choice = input(‘Please type a product number,if type q then exit..‘) if user_choice.isdigit(): user_choice = int(user_choice) if user_choice < len(product_list) and user_choice>=0: choice_list = product_list[user_choice] if salary >= choice_list[1]: # 买得起 shopping_list.append(choice_list) salary = salary - choice_list[1] print(‘Added %s into shopping cart,your balance is\033[31;1m%d\033[0m‘%(choice_list,salary)) else: print(‘Your balance is only \033[31;1m%d\033[0m,not enough..‘%salary) else: print(‘The product number [\033[31;1m%d\033[0m] doesn\‘t exist..‘%user_choice) elif user_choice == ‘q‘: print(‘------------------- shopping list --------------------‘) for mylist in shopping_list: print(mylist) print(‘Your balance is \033[31;1m%d\033[0m‘%salary) exit() else: print(‘Invalid number‘)else: print(‘Please type a integer..‘)
时间: 2024-10-09 19:58:37