python shopping
li = [ {'name':'苹果','price':10}, {'name':'香蕉','price':20}, {'name':'西瓜','price':30} ] shopping_car ={} print('welcome') money = input('How much money you have?:') if money.isdigit()and int(money)>0: for i,k in enumerate(li): print('序号{},商品{},价格{}'.format(i,k['name'],k['price'])) num_choice = input('请输入商品序号: ') if num_choice.isdigit(): if int(num_choice) <= len(li) and int(num_choice)>=0: while 1: num = input('请输入商品数量:') if num.isdigit(): print('你要买的是{},\n数量是{}'.format(li[int(num_choice)]['name'], num)) print('确认购买请输入 Y') con_purchase =input().upper() if con_purchase == 'Y': print('请去结算') if int(money) >= (int(li[int(num_choice)]['price']) * int(num)): money = int(money)-li[int(num_choice)]['price'] * int(num) print('购买成功,你的余额是 {}'.format(money)) break else: print('余额不足,请重新选择数量:') continue else: print('请输入正确的数量:') else:print('请输入正确的商品序号') else:print('没钱请回家拿钱')
posted on 2019-07-24 17:47 LeviDuckle 阅读(143) 评论(0) 收藏 举报
浙公网安备 33010602011771号