Process control if else
if writing a program is compared to walking, so far, we have always taken a straight road and haven't encountered a fork. Imagine that in reality, you encounter a fork, and then you decide where to turn. There must be a motive. You have to judge which fork is the way you really want to go. What if we want the program to handle such judgment? It's very simple. You just need to preset some conditional judgment statements in the program. You can take the fork in whichever condition you meet. This process is called process control.
basically, in all languages, it is implemented with the syntax if... else... And can be divided into single branch, double branch and multi branch
age = 49 if age > 50: print("Older than 50") elif age == 50: print("Age equals 50") else: print("Age less than 50")
you will find that in the above if code, the next line of each condition is indented by 4 spaces. Why? This is a major feature of Python. The purpose of forced indentation is to let the program know which conditions each piece of code depends on. If it is not distinguished by indentation, how can the program know which code to execute when your conditions are established?
In other languages, code blocks are mostly determined by {}. When there is {} to distinguish code blocks, the role of indentation is only to make the code neat.
Python is a super concise language. The inventor must think it's too ugly to use {}, so he simply doesn't use it directly. How can he distinguish code blocks? The answer is forced indentation.
Python's indentation has the following principles:
- The top-level code must be written on the top line, that is, if a line of code itself does not depend on any conditions, it must not be indented
- The indent of codes at the same level must be consistent
- The official advice is to use four spaces for indenting. Of course, you can also use two if you want to be laughed at.
while loop of process control
-
break is used to completely end a loop, jump out of the loop body and execute the statement after the loop
-
Continue is a little similar to break, except that continue only terminates this cycle, and then executes the following cycle, while break completely terminates the cycle
-
Unlike other languages, else is only used with if. In Python, there is a while... Else statement
The else function after while means that when the while loop is executed normally and is not interrupted by break, the statements after else will be executed
import random count = 1 age = None while count <= 10 and (age is None or age < 100): print("------The first", count, "second------") age = int(input("Please enter your age:")) ran = random.uniform(0, 2) if age*ran > 100: print("Your age:", age, ",The random number is", ran, "age×ran=", age*ran, ">100,Exit program") break if age == 0: print("Your age is 0, please re-enter") continue elif age > 50: print("Your age:", age, ",Older than 50,The random number is", ran, "age×ran=", age*ran) elif age == 50: print("Your age:", age, "Age equals 50,The random number is", ran, "age×ran=", age*ran) else: print("Your age:", age, "Age less than 50,The random number is", ran, "age×ran=", age*ran) count += 1 else: if count > 10: print("Executed 10 times, exit") else: print("In case of non-compliance with the rules, exit") print("------out of while loop ------")
output
------1st time------ Please enter your age: 0 Your age is 0, please re-enter ------1st time------ Please enter your age: 11 Your age: 11 less than 50,The random number is 1.9762225675476903 age×ran= 21.738448243024592 ------Second time------ Please enter your age: 50 Your age: 50 is equal to 50,The random number is 1.113568284883348 age×ran= 55.6784142441674 ------3rd time------ Please enter your age: 60 Your age: 60 ,Older than 50,The random number is 1.3357098610902078 age×ran= 80.14259166541247 ------4th time------ Please enter your age: 100 Your age: 100 ,The random number is 1.944775672644064 age×ran= 194.4775672644064 >100,Exit program ------out of while loop ------
...Omit the previous 9 times ------10th time------ Please enter your age: 10 Your age: 10 less than 50,The random number is 0.20069877928471436 age×ran= 2.0069877928471436 Executed 10 times, exit ------out of while loop ------
it can be seen from the output that when break jumps out of the loop, the else statement of while will not be executed!!!
when the loop does not meet the while condition, we will execute the corresponding else statement of while!!!