This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
Two-way judgment, if… else…
In the Python world, we can use the bidirectional form of if: if… The else… To achieve this action: when the number of the remaining wallet is less than 1, need to give a blessing to the person who grabbed the red envelope, came late, but still wish you all the best!!
Now, please run the following code directly to see how the program recognizes:
Print (' redpacketNumber ') else: print(' redpacketNumber ') if redpacketNumber >=1 I'm late, but wish you all the best!! 'Print (' Late, but all the best!! ')Copy the code
In fact, life is the same, a lot of things are not either-or, when not meet the conditions, how to do.
Python helpfully lets us borrow if… Else statement, which gives the coder an alternative — if, otherwise…
In the if… Else conditional statements, if and else are grouped together to form two separate code blocks. Represents a mutually exclusive relationship between a condition and another condition — if the if condition is not satisfied, the else condition is executed.
If = >=1, else = <1.
If redpacketNumber >=1: : print redpacketNumber =0 if redpacketNumber >=1: : print redpacketNumber =0 if redpacketNumber >=1: : print redpacketNumber =0
Else: : print(‘ Late, good luck!! ‘).
If… if… if… else… If the red envelope is more than 50 yuan, we can have a ton of meat, if not more than 50 yuan, we can only eat vegetarian.
You need to assign a value to the amount of money you grabbed. Suppose you grabbed 38 yuan
money=38
# If the red envelope is more than 50, then... (Colon after condition)
if money>50:
Eat too much meat and drink too much wine
print('Eat a lot of meat and drink a lot of wine')
# If the red envelope does not exceed 50, then... (else condition without indentation, condition followed by colon)
else:
# Print: Vegetarianism, health is the absolute truth
print('Vegetarian attention, health is the absolute truth')
Copy the code
About the if… else… If… if… if… if… The else… Statement knowledge point.
In the above eating exercise, we will find that there are not only two sides, there may be a third side, in fact, 38 yuan we can also match some meat dishes, meat and vegetables, more nutritious and healthier.
So, how do you implement three of these scenarios in Python?
Multidirectional judgment: if… Elif… The else…
In the above scenario, we need to use Python’s multidirectional judgment command to determine three or more conditions: if… Elif… The else… .
These three constitute the command logic relation of multidirectional judgment: if the condition of if is not satisfied, we will see whether the condition of ELIF is satisfied in order; if the condition of ELIF is not satisfied, we will execute the command of else.
In addition, when more than three conditions are judged, elIF can be used for all the intermediate conditions.
So, how do I write this code? Let’s get a feel for elif’s logic in code
You need to assign a value to the amount of money you grabbed. Suppose you grabbed 38 yuan
money=38
# If the red envelope is more than 50, then... (Colon after condition)
if money > 50:
Eat too much meat and drink too much wine
print('Eat a lot of meat and drink a lot of wine')
# If the red envelope exceeds 35, the red envelope does not exceed 50
elif 50 >= money > 35:
print('More nutritious and healthy with meat and vegetables')
# If the red envelope does not exceed 50, then... (else condition without indentation, condition followed by colon)
else:
# Print: Vegetarianism, health is the absolute truth
print('Vegetarian attention, health is the absolute truth')
Copy the code
With the accumulation of if and else above, is the logic of multidirectional judgment elif easy to understand?
First of all, the assignment of the first row will be tried one by one from top to bottom to see which condition it satisfies. If it satisfies, it will not go down. If it does not satisfy, it will continue to try and get the result.
Second, the elif operation is essentially the same as the else operation, which already has the meaning of other conditions, so the elif can not be followed by the else.
For example, in the code above, Money =38 scans each of the following if elif else conditions to see which one it satisfies and executes the command under that condition.
Elif 50 >= money > 35: print(‘ more healthy, more healthy ‘)
Here’s a summary of elif’s knowledge: