python flow control - conditional statements If, while loop

A, If, conditional statements - Select

Format: python simple and beautiful, attention indent

1. The first:

if conditions:

Four spaces (tab key) satisfies the condition during step

if 5>4 :
    print(666)
print(777)

2. The second:

if conditions:

Four spaces (tab key) satisfies the condition during step

else:

Perform steps of four spaces (tab key) does not satisfy the conditions

3. The third (multiple choice):

if Condition 1:

Four spaces (tab key) satisfies the conditions of step 1

elif Condition 2:

Four spaces (tab key) satisfies the conditions of step 2

..............

else:

When the execution condition is not satisfied in step

4.if nesting

. 1 name = INPUT ( ' Enter name: ' )
 2 Age = INPUT ( ' Enter Age: ' )
 . 3  
. 4  IF name == ' small two ' :
 . 5      IF Age == ' 18 is ' :
 . 6          Print (666 )
 . 7      the else :
 . 8          Print (333 )
 . 9  the else :
 10      Print ( ' wrong .... ' )

Two, while circulation

while conditions:

  Loop

1  Print ( ' 111 ' )
 2  the while True:
 3      Print ( ' not like us ' )
 4      Print ( ' in the world ' )
 5      Print ( ' itch ' ) # an infinite loop ctrl + C to terminate the cycle, the forced exit
 6  Print ( ' 222 ' )

Terminator Cycle: change the conditions so that it does not hold.

exe: Printing from 1 to 100

count = 1
flag = True
#标志位
while flag:
    print(count)
    count = count + 1
    if count > 100 :
        flag = False


count = 1
while count <= 100:
    print(count)
    count = count + 1


count = 1
sum = 0

while count <= 100:
    sum = sum + count 
    count = count + 1
    
print(sum)

Terminator Cycle: BREAK (direct out of the loop)

 1 print('11')
 2 while True:
 3     print('222')
 4     print(333)
 5     break
 6     print(444)
 7 print('abc')
 8 
 9 count = 1
10 while True:
11     print(count)
12     count = count + 1
13     if count > 100:break

continue: terminate this cycle, continue to the next cycle (equivalent to the bottom)

1 print(111)
2 count = 1
3 while count < 20 :
4     print(count)
5     continue
6     count = count + 1
1 count = 0
2 while count <= 100 : 
3     count += 1
4     if count > 5 and count < 95: 
5         continue 
6     print("loop ", count)
7 
8 print("-----out of while loop ------")

 

Guess you like

Origin www.cnblogs.com/RevelationTruth/p/11439861.html