首页 > 代码库 > python3 流程控制

python3 流程控制

表达式if ... else

>>> if 3 > 4:
...     print(‘False‘)
... else:
...     print(‘True‘)
... 
True

  

表达式if ... elif...else

>>> a = 10
>>> if a > 15:
...     print("ok")
... elif a < 15:
...     print("good")
... else:
...     print("what")
... 
good

  

循环while

while True:
...   a = input(‘>>>‘).strip()
...   b = 10
...   c = int(a)
...   if c > b:
...     print(‘go on‘)
...     continue
...   else:
...     print(‘ok‘)
...     break

  

循环for loop

>>> for i in range(10):
...     print(‘loop:‘,i)
... 
loop: 0
loop: 1
loop: 2
loop: 3
loop: 4
loop: 5
loop: 6
loop: 7
loop: 8
loop: 9

  

>>> for i in range(10):
...   if i >8:
...     break
...   print(‘loop:‘,i)
... 
loop: 0
loop: 1
loop: 2
loop: 3
loop: 4
loop: 5
loop: 6
loop: 7
loop: 8

  

python3 流程控制