Python from 0 to 1 (day 15) - Python conditional judgment 2

2022-02-01 04:23:10

This is my participation 11 The fourth of the yuegengwen challenge 20 God , Check out the activity details ：2021 One last more challenge

Two way judgment ,if...else...

stay Python The world , We can use if Two way judgment form ：`if…else…` To achieve this action ： When the number of remaining wallets is less than 1 when , We need to send blessings to those who rob red envelopes , Late. , But I wish you all the best !!!

below , Please run the following code directly , See how the program recognizes ：

``````#  Number of remaining red envelopes

redpacketnumber=0

#  Conditions ： If the number of remaining red envelopes is greater than or equal to 1

if redpacketnumber >=1:

#  result ： Show ‘ Successfully grabbed the red envelope ’ Result

print(' Successfully grabbed the red envelope ')

else:

#  result : Show ' Late. , But I wish you all the best !!!' Result

print(' Late. , But I wish you all the best !!!')
Copy code ``````

In fact, life is the same , Many things are not either or , When the conditions are not met , What are we going to do .

Python It's very sweet , Let's borrow if...else sentence , Let the yard farmer have another choice ---[ If ... Just , otherwise ... Just ]

stay if...else In a conditional statement ,if and else Hold each other together , Form two different code blocks . And other conditions are mutually exclusive —— If not satisfied if Conditions , Is executed else Other conditions .

If we use the example of robbing red envelopes above to explain ,if The definition is the number of remaining red envelopes >=1, and else The definition is the number of remaining red envelopes <1.

And because of the assignment 【`redpacketnumber =0`】, Not satisfied 【`if redpacketnumber >=1:`】 This condition , So we can't execute 【`print(' Successfully grabbed the red envelope ')`】 The order of .

Only the second way else: Way —— perform `print(' Late. , I wish you all the best !!!')` Result .

below , Let's do a problem to consolidate if...else... Knowledge points of : Grab the red envelope , We can go to dinner , But what to eat still has to be decided according to the number of red envelopes , If we grab more red envelopes than 50 element , We can have a ton of meat , If not more than 50 element , We can only be vegetarian .

``````#  First assign a value to the amount of the red envelope , Suppose you get 38 element

money=38
#  If the red envelope exceeds 50 Under the condition of , Just ...( A colon is required after the condition )

if money>50:

#  Just print the results : Take a big bite of meat , Drink heavily

print(' Take a big bite of meat , Drink heavily ')

#  If the red envelope does not exceed 50, Just ...（else No indent before condition , A colon is required after the condition ）

else:

#  Just print : Vegetarianism , Health preservation is the last word

print(' Vegetarian attention , Health preservation is the last word ')
Copy code ``````

About if...else... That's all for you , Let's summarize if…else… Knowledge points of statements .

In the practice of eating above , We will find that , It's not just two sides , There may be a third side , Actually 38 Yuan, we can also go with some meat dishes , Meat and vegetable collocation , More nutrition , Healthier .

that , There are three in the scene if Python How to achieve it ？

Multidirectional judgment ：if…elif…else…

In the above scenario , In judging 3 Or 3 More than one condition , We need to use Python Multidirectional judgment command in ：if…elif…else….

These three together constitute the command logic relationship of multi-directional judgment ： If if The condition of is not satisfied , It depends on the order of satisfaction elif Conditions , If not satisfied elif Conditions , Is executed else The order of .

also , When the condition of judgment exceeds 3 Time , Multiple conditions in the middle can be used elif.

that , How to write this code ？ Let's feel it in the code elif The logic of

``````#  First assign a value to the amount of the red envelope , Suppose you get 38 element

money=38

#  If the red envelope exceeds 50 Under the condition of , Just ...( A colon is required after the condition )

if money > 50:

#  Just print the results : Take a big bite of meat , Drink heavily

print(' Take a big bite of meat , Drink heavily ')

#  If the red envelope exceeds 35, Not more than 50

elif 50 >= money > 35:

print(' Meat and vegetable collocation , More nutrition , Healthier ')

#  If the red envelope does not exceed 50, Just ...（else No indent before condition , A colon is required after the condition ）

else:

#  Just print : Vegetarianism , Health preservation is the last word

print(' Vegetarian attention , Health preservation is the last word ')
Copy code ``````

Pass above if and else The accumulation of , Multidirectional judgment elif Is it easy to understand the logic of ?

First , The assignment of the first line , I'll try it one by one from top to bottom , See what conditions you meet , If you are satisfied, don't go down , If you are not satisfied, continue to try , Until you get the results .

secondly ,elif The operation of is essentially the same as else equally , Has the meaning of other conditions , therefore elif You can not answer else.

Like the code above ,money=38 Will be on the following if elif else Scan conditions one by one , See which one you satisfy , Just execute the command under which conditions .

Obviously , The second condition elif 50 >= money > 35 Just like money=5 Match the premise of , therefore , perform elif The following command : `print(' Meat and vegetable collocation , More nutrition , Healthier ')`

below , Let's summarize elif Knowledge points of ：