一、 从第3层循环直接跳出所有循环
break_flag = False count = 0 while break_flag == False: print("-第一层") while break_flag == False: print("第二层") while break_flag == False: count += 1 if count > 10: break_flag = True print("第三层") print("keep going...")
with break_flag
# break_flag = False count = 0 while count < 3: print("-第一层") while count < 3: print("第二层") while count <= 10: count += 1 # if count > 10: # break_flag = True print("第三层") print("keep going...")
without break_flag
二、 购物车程序
goods_list = [[‘Iphone7‘,5800],[‘Coffee‘,30],[‘Book‘,99],[‘Bike‘,199],[‘Vivi X9‘,2499]] #商品列表 shopping_cart = [] #用户购物车列表 salary = int(input(‘input your salary:‘)) #用户薪水 m = salary k = 0 while True: index = 0 for goods in goods_list: #打印商品列表 print(index,goods) index += 1 choice = input(‘>>>:‘).strip() if choice.isdigit(): choice = int(choice) if choice >= 0 and choice < len(goods_list): goods = goods_list[choice] if goods[1] <= salary: #判断用户是否带了足够的钱来支付所选商品 shopping_cart.append(goods) salary -= goods[1] print(‘Add goods ‘+str(goods[0])+‘ into shopping cart! Your current balance:‘+str(salary)) else: print(‘No enough money!The price is:‘+str(goods[1])+‘! Need more:‘+str(goods[1]-salary)) else: print(‘Have no this goods!‘) elif choice == ‘q‘: print(‘----------Your shopping cart----------‘) print(‘ID goods quantity price total‘) for i in range(len(goods_list)): j = shopping_cart.count(goods_list[i]) if j > 0 : k += 1 print(k,‘\t‘,goods_list[i][0],‘\t‘,j,‘\t\t‘,goods_list[i][1],‘\t‘,j * goods_list[i][1]) print(‘Total price is:‘,m - salary) break else: print(‘Have no this goods‘)
购物车程序
时间: 2024-12-19 03:56:03