While loop
1. Basic cycle
123456 |
while 条件: # 循环体 # 如果条件为真,那么循环体则执行 # 如果条件为假,那么循环体不执行 |
2. Break
Break to exit all loops
1234 |
while True : print "123" break print "456" |
3, continue
Continue is used to exit the current loop and continue the next loop
1234 |
while True : print "123" continue print "456" |
Exercises
1. Use while loop input 1 2 3 4 5 6 8 9 10
2. For all numbers of 1-100
3. All odd numbers in output 1-100
4. All even numbers in output 1-100
5, Beg 1-2+3-4+5 ... 99 of all numbers of the and
6. User Login (three chance retry)
1. Use while loop input 1 2 3 4 5 6 8 9 10
n = 1
While N < 11:
if n = = 7:
Pass
Else
Print (n)
n = n + 1
Print ('----end----')
2. For all numbers of 1-100
n = 1
s = 0
While N < 101:
s = s + N
n = n + 1
Print (s)
3. All odd numbers in output 1-100
n = 1
While N < 101:
TEMP = n 2
If temp = = 0:
Pass
Else
Print (n)
n = n + 1
Print ('----end----')
4. All even numbers in output 1-100
n = 1
While N < 101:
TEMP = n 2
If temp = = 0:
Print (n)
Else
Pass
n = n + 1
Print ('----end----')
5, Beg 1-2+3-4+5 ... 99 of all numbers of the and
n = 1
s = 0 # s is the sum of all previous numbers
While n < 100:
TEMP = n 2
If temp = = 0:
s = N-S
Else
s = s + N
n = n + 1
Print (s)
Python Learning Path (7)--while loop