Python教程 跳出多層迴圈for while

2022-06-06 04:27:09 字數 2692 閱讀 5927

for跳出多層迴圈

break_flag = false

for i in range(10):

print("爺爺層",i)

for j in range(10):

print("=爸爸層",j)

if j == 3:

break_flag = true

break

for k in range(10):

print("===>孫子層",k )

if k == 2:

break_flag = true

break

if break_flag:

break

if break_flag: #if break_falg == true:

print("我兒子死了,我也不活了..")

break

print("keep going....")

while跳出多層迴圈

#python學習交流qq群:778463939

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....")

跳出多級選單

map=,

"華東":

}flag = false

while flag == false:

print("中國有如下地區:")

for i in map.keys():

print(i)

area = input("請選擇乙個地區、b或者q:").strip()

if area == "b":

break

if area == "q":

flag = true

if len(area) == 0:

continue

if area in map.keys():

while flag == false:

print("該地區有如下省:")

for j in (map[area]).keys():

print(j)

province = input("請選擇乙個省、b或者q:").strip()

if province == "b":

break

if province == "q":

flag = true

if len(province) == 0:

continue

if province in (map[area]).keys():

while flag == false:

print("該省有如下城市:")

for j in map[area][province]:

print(j)

city = input("請選擇b或者q:")

if city == "b":

break

if city == "q":

flag = true

if len(city) == 0:

continue

跳出多層迴圈簡化版

menu = ,

'網易':{},

'google':{}

},'中關村':,

'汽車之家':{},

'youku':{},

},'上地':,

},'昌平':,

'北航':{},

},'天通苑':{},

'回龍觀':{},

},'朝陽':{},

'東城':{},

},'上海':}},

'閘北':}},

'浦東':{},

},'山東':{},

}last_layers = [ menu ] #上一層

current_layer = menu #當前層

while true:

for key in current_layer:

print(key)

choice = input(">>:").strip()

if len(choice)==0:continue

if choice in current_layer: #進入下一層

current_layer = current_layer[choice] #北京

if choice == "b":

if last_layers:

current_layer = last_layers[-1] #取到上一層,賦值給current_layer

last_layers.pop()

if choice == 'q':

break

flag跳出多層迴圈

有時候我們需要從第三層的for迴圈直接跳出第二層的while迴圈,進入第一層迴圈,只用break就做不到了,這時候我們可以用flag.1 用法1 定義標識為ture,然後while flag,比如驗證登入資訊,驗證成功直接改變標識狀態為false,直接跳到第一層while迴圈 不執行else裡的 如...

跳出多層迴圈for while

break flag false for i in range 10 print 爺爺層 i for j in range 10 print 爸爸層 j if j 3 break flag true break for k in range 10 print 孫子層 k if k 2 break f...

如何指定跳出多層迴圈

public class outloopwithlabel 方法二 boolean,break組合 通過boolean型條件變數和break的組合,由內層迴圈在跳出 break 前改變條件變數,外層迴圈檢測條件變數改變時終止外層迴圈。public class outloopwithbooleanbr...