标签:int 商品列表 bsp logs 启动 end 清单 else python
# readme: # ne_zha # blogger address:www.cnblogs.com/ne-zha # 要求: # 1.启动程序,让用户输入存款,然后打印商品列表 # 2.允许用户根据商品编号选择购买商品 # 3.用户选择完商品后检测余额是否足够扣款,满足提示扣款成功,不满足提示余额不足 # 4.可随时退出,退出时打印购买商品和余额 savings =input(‘请输入您的存款:‘) commodity_list =[(‘iphone7‘,5888),(‘bike‘,1500),(‘huaweiP9‘,3666),(‘headset‘,199)] if savings.isdigit(): savings =int(savings) while True: for item in enumerate(commodity_list): print(item) user_choice =input(‘请输入您想要的商品编号:‘) if user_choice.isdigit(): user_choice =int(user_choice) if user_choice < len(commodity_list) and user_choice >= 0: choice = commodity_list[user_choice] price_choice =choice[1] if price_choice<=savings: buy_user =[] buy_user.append(choice[0]) print(‘商品\033[1;31;40m%s\033[0m已加入购物车‘%choice[0]) savings-=price_choice print(‘余额:\033[1;31;40m%s\033[0m‘%savings) else: print(‘余额不足,购买失败!‘) else: print(‘商品输入有误,请重新输入:‘) elif user_choice ==‘q‘: print(‘您已买的商品清单与余额:‘) for i in buy_user: print(‘\033[1;31;40mi\033[0m) print(‘\033[1;31;40msavings\033[0m‘) exit() else: print(‘invalid option‘)
标签:int 商品列表 bsp logs 启动 end 清单 else python
原文地址:http://www.cnblogs.com/ne-zha/p/7153432.html