2, while circulation

1, while circulation

while conditions:

         Code block (loop)

Implementation process:

1, it is determined whether a condition is true, if true, the code block is executed

2, the determination condition again, if true continue to block

3, until the judgment is false, stop

Example 1:

the while True: 
    Print ( " Hello, I'm Chow Yun Fat " ) 
    Print ( " Hello, I am your father " )

Under this condition is always true, it would be an infinite loop

 

Example 2:

= COUNT 1 
the while COUNT <= 8 : 
    Print ( " Hello, I'm Chow Yun Fat " ) 
    Print ( " Hello, I am your father " ) 
    COUNT = COUNT + 1

2, break the current cycle stop

# Allows users to spray, but when the input "q" exit

Example 1:

the while True: 
    S = INPUT ( " Please enter Content: " )
     IF S == " Q " :
         BREAK 
    Print ( " content of the spray is: " + S)

 

3, continue, stop the current cycle, the next cycle continues

Example 2:

the while True: 
    s = the INPUT ( " Please enter the content: " )
     IF s == ' q ' :
         BREAK 
    IF  " Ma "  in s: s # variable contains " Ma " character 
        Print ( " you output sensitive character, please re-export : " )
         Continue 
    Print ( " content of the spray is: " + S)

 

 

 

5, summing cycle training

Lists 100;

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

1+2+3+4+5+....+100=?

= count . 1 
sum = 0 
the while count <= 100 : 
   # Print (count) 
    sum = sum + count # of the sum (the result of the previous operation) and the current count adding 
    count = count + . 1 
    Print (sum)

 

 

 

# Outputs odd 1-100

count = 1
while count <= 100:
    if count % 2 !=0:
        print(count)
    count = count + 1

 

 

 

6, the output format

'' '
Print ( "Cao Cao sex male, 58 years old this year, is a goat, loving woman")
Print ( "Diao Chan Sex female, 16 years of party, is a goat, loving man")
Print ( "Yaojin Gender Male, 49 years old this year, he is a goat, loving woman "
'' '
Example 1:

name = the INPUT ( " Enter name: " ) 
Age = the INPUT ( " Please enter the Age: " ) 
Hobby = the INPUT ( " Please enter the hobby: " ) 
max = the INPUT ( " Please enter the sex: " ) 

Print (name + " gender " + max + " this year " + Age + " years old, is a goat, hobbies " + Hobby)

 

 

 

Example 2:

% S: placeholder (string type)

% D: placeholder (integer)

name = the INPUT ( " Enter name: " ) 
Age = the INPUT ( " Please enter the Age: " ) 
Hobby = the INPUT ( " Please enter the hobby: " ) 
max = the INPUT ( " Please enter the sex: " ) 
Print ( " % S Sex % s% s years old this year, he is a goat, hobby% s " % (name, max, Age, Hobby))

 

 

 Example 3:

= A 108 
Print ( " Liangshan hero has% d " % (A))

 

 

= A 108 
Print ( " Liangshan hero has% d, 10% of which is the woman " % (A))

 

 

= A 108 
Print ( " Liangshan hero has% d, where 10 is the woman %% " % (A)) # numbers when there is an escape% to not add \% was added

 

Guess you like

Origin www.cnblogs.com/y58-jw/p/11598498.html