如果for-else中 for循環(huán)正常死亡 那么會執(zhí)行else 如果for-else中的for循環(huán)中有continue 也會執(zhí)行else中代碼,但是如果是break 將不再執(zhí)行else中代碼
for-else
如果for和else 配合使用的時候 當for循環(huán)執(zhí)行完成后 再執(zhí)行else中的代碼 然后在打印測試
for i in range(5):
print(i)
else:
print("for-else")
print("測試")
for-else 如果for循環(huán)中有了break
for i in range(5):
print(i)
if i == 2:
break
else:
print("for-else")
print("測試")
for-else 中for循環(huán)中有continuee
for i in range(5):
if i == 2:
continue
print(i)
else:
print("for-else")
print("測試")
while-else 和for-else 效果一樣的
i = 0
while i < 5:
# if i == 2:
# # break
print(i)
i += 1
else:
print("else")
print("測試")