1. Process control theory
Process control (also known as control process) means program When running, individual instructions (or statements subroutine )Run or evaluation The order of. That is, the execution process of the control program. The execution process is divided into: sequential structure (all the codes we wrote before are sequential structure), branch structure (using if judgment) and loop structure (using while and for)
Let me give you a rough flow chart of the execution process:
Sequential structure:
Branch structure:
Cycle structure:
Among them, let's talk about the branch structure and loop structure:
Why are there different structures? Because for many cases, the code of sequential structure is far from enough. For example, a program is limited to adults, and children have no permission to use it because they are not old enough. At this time, the program needs to make a judgment to see whether the user is an adult and give a prompt. stay Python In, you can use if else statements to judge conditions, and then execute different codes according to different results, which is called selection structure or branch structure.
2. if judgment of branch structure
If else statements in Python can be subdivided into three forms: if statement, if else statement and if elif else statement.
If: if the expression holds (true), execute the following code block; If the expression does not hold (false), nothing is executed.
'Syntax structure: if Conditions: 'Subcode executed after the condition is established (multiple lines are allowed) eg: Defines whether a person is known to have reached adulthood age=19 if age> 18 #branch print('Grown up')
Else: if the expression holds, execute code block 1 immediately following if; If the expression does not hold, execute the code block 2 immediately following else
"""" Grammatical structure if condition: Subcode executed after the condition is established(There can be multiple lines) else: Sub code executed when the condition is not tenable(There can be multiple lines) """" # If a person is older than 18, he is an adult, otherwise he is an adult. age=19 if age > 18: print('Grown up') else: print('under age')
Elif: Python will judge whether the expression is valid one by one from top to bottom. Once a valid expression is encountered, it will execute the following statement block; At this point, the rest of the code is no longer executed, regardless of whether the following expression is true or not. If all expressions fail, execute the code block after else
""" Grammatical structure if Condition 1: Sub code executed when condition 1 is true(There can be multiple lines) elif Condition 2: Sub code executed when condition 1 is not true and condition 2 is true(There can be multiple lines) elif Condition 3: Conditions 1 and 2 are not true, and condition 3 is true. It is the sub code executed(There can be multiple lines) else: All of the above conditions are not tenable, and the sub code is executed(There can be multiple lines) middle elif There can be more than one """ #3.3. If the name is BOSS, print the BOSS. If it is manager, print the manager. If it is charge, print the supervisor. If it is other, print the employee username = input('username>>>:') if username == 'BOSS': print('boss') elif username == 'Manager': print('manager') elif username == 'charge': print("executive director") else: print('staff') #Nesting of if: eg: Go to the Internet cafe to surf the Internet do_business = True moeny < 10: if do_business: print('Welcome to our online cafe') if moeny < 10: print('There's not enough money, you loser') else: print('Recharge succeeded') else: print('Go home and sleep')
3. while loop of loop structure
""" Grammatical structure while condition: The loop body code executed after the condition is established 1.First judge whether the condition is true. If it is true, execute the loop body code 2.After the execution of the loop body code, judge whether the condition is true again. If it is true, continue to execute the loop body code. If not, jump out of the loop """ while True: # 1. Obtain user name and password username = input('username>>>:') password = input('password>>>:') # 2. Verify / judge whether the user name and password are correct if username == 'TONY' and password == '123': print('Login successful') else: print('Wrong user name or password') # while + break break It is used to directly end the cycle of this layer """ break You can only end the loop on the layer where you are """ while True: # 1. Obtain user name and password username = input('username>>>:') password = input('password>>>:') # 2. Verify / judge whether the user name and password are correct if username == 'TONY' and password == '123': print('Login successful') break # End the cycle of this layer else: print('Wrong user name or password') # while + continue continu It is used to end this cycle and directly start the next cycle count = 1 while count < 11: if count == 4: count += 1 continue # End this cycle and start the next cycle '''Jump directly to the place of condition judgment and execute again''' print(count) count += 1
Today's content supplement
debug pattern You can turn code that is executed instantaneously into step-by-step execution
-
Break point: on the right side of the line number of the code block to be debugged, left click to display a red dot mark, which is the breakpoint. Click again to cancel the breakpoint.
-
Right click the editing area and click the Debug button; Or select the running file in the toolbar and click the Debug icon button.
-
The Debug console is displayed. The console has two display panels, Debugger and console. Look at Debugger first.
-
Click the Step Over button to start step-by-step debugging. Each click will skip one step. And display the content in the interpretation area
Must know must know:
1.stay python Indents are used in to indicate code dependencies if 18 > 19: print('Hey, hey, hey') # The if judgment determines whether to execute or not """We also call the indented code the sub code of so and so""" 2.Not all code can have subcodes(Subordinate code) Currently available if else 3.If multiple lines of code belong to the same parent code, they need to ensure the same indentation stay python Four spaces are recommended for indenting in(Four look better) 4.Code with the same amount of indentation is executed in a sequential structure with no subordination to each other """ps:If a line of code ends with a colon, the next line must be indented The meaning of colon is equivalent to having subcode """