today we look at conditional statements and loop statements.
Preview:
1. Using while loop output 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)
First, conditional statements
When we write a program that requires branching, we can also say that our conditional statements are used when an event occurs in a particular situation that produces a different processing condition.
If...else statement:
Single branch:
1 "' 2 If Condition: 3 code executed after satisfying the condition 4 ' ' 5 6 age = 187 if-= = 18:8 print (" I am a grown man! ")
Dual Branch :
1 "2 If condition: 3 code executed after satisfying the condition 4 else 5 does not meet if when executing 6 ' ' 7 8 ' age = 9 If the age <= 18:10 print (" I am not years! Else:12 Print ("I am a grown man!") ")
Multi-branch:
1 "2 If condition: 3 code executed after satisfying the condition 4 elif Condition: 5 does not meet the above conditions to execute 6 elif condition: 7 does not meet the above conditions to perform 8 ... 9 Else10 does not meet the above conditions to execute one "", "the Age = 1914 if " <= 18:15 print ("I am not yet years! ") Elif age >= 18:17 print (" I'm grown! else:19 Print ("I'm just grown up this year!") ")
Indent:
In other languages, most of the code blocks are identified by {}, and Python does not have {} which is a feature of Python. How does python determine which code block to execute? This leads to a concept forced indentation , which is intended to let the program know which condition each piece of code depends on, and if it does not differentiate by indentation, the program cannot determine which code block is executing.
The Python indentation principle:
The top-level code must write the top line, i.e., if a line of code itself does not depend on any condition, it must not be indented
Same level of code, indentation must be consistent
The official recommendation is to indent with 4 spaces, and of course you can indent in the way you used to.
Second, the circular statement
While statement:
1 "2 While condition: 3 code executed after satisfying the condition 4" ' 5 6 count = 0 7 while count <=: #只要count <=100 constantly executes the following code 8 print ("l Oop ", count) 9 count +=1 #每执行一次, put the count+1, or it will become a dead loop, because count has always been 0
While...else statement:
Unlike other languages else, which is usually only the same as if, there is a while ... else statement in Python. The else function behind the while loop is that the statement after the else is executed when the while cycle is executed properly and the middle is not interrupted by a break.
Dead Loop:
There is a cycle called death cycle, one but into the dead loop, the program will run to the everlasting forever can not quit.
The while is always executed as long as the condition behind it is always set (i.e. the condition result is always true).
For example: The above code, if there is no code count + = 1, the program will go into the dead loop. Count <= 100 is always set because count = 0.
Loop Termination statement:
If, for some reason, you do not want to continue looping in the process of looping, you will need to break or continue to terminate the statement.
Break : completely jump out of the loop and execute the code after the loop.
Continue: jump out of this cycle, do not execute continue after the code, re-enter the loop to cycle the condition of the judgment.
For loop:
1 for I in range (4): # i is variable (4) value range 2 print (">>:", i) # 0 1 2-4 for I in Range (1,5): # Gu Tou regardless of tail 5 C4/>print (">>:", i) # 1 2 3 7 for I in Range (1,5,2): # step 2 Each two take a value of 8 print (">>:", i) # 1 3
99 Multiplication Table Exercises:
1 for I in Range (1,10): 2 for J in Range (1,i+1): 3 print ("%s*%s=%s"% (j,i,i*j), end= "") 4 print ()
Results:
Preview Answer:
1 #使用while循环输出1 2 3 4 5 6 8 9 2 count = 1 3 while count <= 10:4 Print (count) 5 Count + = 1 6 if cou NT = = 7:7 count + = 1 8 9 #count = 010 #while count < 10:11 # count + =, # if Count = = 7:13 # CONTINUE14 # Print (count)
1 #求1-100 of all numbers and 2 count = sum = while count <= 100:5 sum + = Count6 count + + print (sum)
1 #输出 1-100 all odd 2 count = while count <= 100:4 Print (count) 5 count + = 2
1 #输出 1-100 All even 2 count = Full while Count <= 100:4 Print (count) 5 count + = 2
1 #求1 -2+3-4+5 ... 99 of all numbers and 2 count = 1 3 sum = 0 4 while count < 100:5 if count% 2 = = 1:6 sum + = Count 7 else:8 su M-= Count 9 count + = print (sum)
1 #用户登陆 (three chance retry) 2 username = "Oldbody" 3 Password = 10086 4 count = 1 5 print ("Please enter the account password for a total of three attempts!") ") 6 while Count <= 3:7 name = input (" Please enter account: ") 8 pswd = Int (input (" Enter password: ")) 9 if name = = Username and PS WD = = Password:10 print ("input correct!") ") One break12 else:13 print (" ", Count," input error please re-enter!) ") count + = 1
Small Knowledge Points:
Print () comes with a newline character.
If you want to cancel the default newline multibyte End (""), please refer to the code of the 99 multiplication table.