while循環(huán)
while是python中執(zhí)行循環(huán)的一個重要方法.只要條件是true,while語句會不斷重復執(zhí)行下去.
while循環(huán)的基本語法如下:
while test:
code statements
else:
final code statements
實例
x = 0
while x < 10:
print('x is currently: ',x)
print(' x is still less than 10, adding 1 to x')
x+=1
執(zhí)行結(jié)果:
x is currently: 0
x is still less than 10, adding 1 to x
x is currently: 1
x is still less than 10, adding 1 to x
x is currently: 2
x is still less than 10, adding 1 to x
x is currently: 3
x is still less than 10, adding 1 to x
x is currently: 4
x is still less than 10, adding 1 to x
x is currently: 5
x is still less than 10, adding 1 to x
x is currently: 6
x is still less than 10, adding 1 to x
x is currently: 7
x is still less than 10, adding 1 to x
x is currently: 8
x is still less than 10, adding 1 to x
x is currently: 9
x is still less than 10, adding 1 to x
請仔細看執(zhí)行結(jié)果進行了多少次打印以及在條件x == 10滿足前,while循環(huán)共進行了多少次.
我們在上面的代碼基礎(chǔ)上加上else
x = 0
while x < 10:
print('x is currently: ',x)
print(' x is still less than 10, adding 1 to x')
x+=1
else:
print('All Done!')
執(zhí)行結(jié)果:
x is currently: 0
x is still less than 10, adding 1 to x
x is currently: 1
x is still less than 10, adding 1 to x
x is currently: 2
x is still less than 10, adding 1 to x
x is currently: 3
x is still less than 10, adding 1 to x
x is currently: 4
x is still less than 10, adding 1 to x
x is currently: 5
x is still less than 10, adding 1 to x
x is currently: 6
x is still less than 10, adding 1 to x
x is currently: 7
x is still less than 10, adding 1 to x
x is currently: 8
x is still less than 10, adding 1 to x
x is currently: 9
x is still less than 10, adding 1 to x
All Done!
break, continue, pass
如果我們的實際問題需要解決多種條件.可以在循環(huán)里使用break, continue, pass解決問題.
break指終止正在進行的最近的循環(huán).
continue指回到最近的循環(huán)的開頭.
pass指什么都不做.
加上break, continue,while循環(huán)的語法如下:
while test:
code statement
if test:
break
if test:
continue
else:
break和continue可以放在循環(huán)體里的任何位置,但是我們經(jīng)常把它們放在嵌套if語句后面以在某種條件下執(zhí)行操作.
實例
x = 0
while x < 10:
print('x is currently: ',x)
print(' x is still less than 10, adding 1 to x')
x+=1
if x==3:
print('x==3')
else:
print('continuing...')
continue
執(zhí)行結(jié)果如下:
x is currently: 0
x is still less than 10, adding 1 to x
continuing...
x is currently: 1
x is still less than 10, adding 1 to x
continuing...
x is currently: 2
x is still less than 10, adding 1 to x
x==3
x is currently: 3
x is still less than 10, adding 1 to x
continuing...
x is currently: 4
x is still less than 10, adding 1 to x
continuing...
x is currently: 5
x is still less than 10, adding 1 to x
continuing...
x is currently: 6
x is still less than 10, adding 1 to x
continuing...
x is currently: 7
x is still less than 10, adding 1 to x
continuing...
x is currently: 8
x is still less than 10, adding 1 to x
continuing...
x is currently: 9
x is still less than 10, adding 1 to x
continuing...
x = 0
while x < 10:
print('x is currently: ',x)
print(' x is still less than 10, adding 1 to x')
x+=1
if x==3:
print('Breaking because x==3')
break
else:
print('continuing...')
continue
輸出結(jié)果:
x is currently: 0
x is still less than 10, adding 1 to x
continuing...
x is currently: 1
x is still less than 10, adding 1 to x
continuing...
x is currently: 2
x is still less than 10, adding 1 to x
Breaking because x==3