Day2.跳出多层循环+购物车
购物车程序:
# 1)你可以买下面的东西
# 2)放入购物车,扣钱,同事打印余额
# 3)您买了下面的东西
# 4)您还有多少钱
# 2)放入购物车,扣钱,同事打印余额
# 3)您买了下面的东西
# 4)您还有多少钱
product_list=[["苹果",10],["椰子",200],["菠萝",20],["火龙果",50],] shopping_cart=[] salary=int(input("请输入你的薪资:")) while True: index=0 for product in product_list: print(index,product) index+=1 choice=input(">>>:") if choice.isdigit(): choice=int(choice) if choice>= 0 and choice<(len(product_list)): product=product_list[choice] if salary >= product[1]: shopping_cart.append(product) salary -= product[1] print(product[0]+"已成功购买,您还剩余"+str(salary)+"元钱") else: print("余额不足,还差"+str(product[1]-salary)+"元钱才能购买"+product[0]) else: print("商品不存在!") elif choice == "q": print("-------商品列表-------") for i in shopping_cart: print(i) print("您的余额为:"+str(salary)) print("---------end----------") break else: print("不能识别该选项")
while循环中跳出多层循环代码如下:
1 flag_break = False 2 count = 0 3 while flag_break == False: 4 print("---aaa") 5 while flag_break == False: 6 print("--bb") 7 while flag_break == False: 8 count+=1 9 if count > 5: 10 flag_break = True 11 print("-c") 12 print("chulaile!!!")
for循环中跳出多层循环代码如下:
1 flag_break = False 2 for i in range(5): 3 print("-aaa") 4 for j in range (5): 5 print ("--bbb") 6 if j == 3: 7 flag_break = True 8 break 9 for k in range (5): 10 print ("---ccc") 11 if k == 2: 12 flag_break = True 13 break 14 if flag_break: 15 break 16 if flag_break: 17 break 18 print ("chulaile")