This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

When we use wireless earphones to listen to music, we can set two taps on the earphones to play the next song. If I just set the headset to play the next song with two taps, then, due to the lack of a prerequisite that the headset is in use (the headset plugs into the ear as if it is in use), two taps will play the next song regardless of whether you are in use.

So, our setup should look like this: tap twice and play the next track if the headphones are in use.

And in this setting if… Just… Logic, is to communicate with the computer logic-conditional judgment, its role is clear to let the computer know: under what conditions, should do what.

The same is true for Python, where Python can perform automated tasks, such as successfully retrieving data information we specify in the Python crawler project, because it can perform conditional judgments.

Next, let’s get a taste of the fun of logical judgment through one of China’s new four great inventions: “Scan payment”.

Two-dimensional code payment sounds like a very new technology, in fact, this is similar to mobile newspaper, not new technology. As early as the 1990s, two-dimensional code payment technology has been formed, but in China is just emerging in recent years.

In the Spring Festival of 2014, wechat red envelope was launched, laying a user base for scanning code payment.


import random
print("Please enter the amount of the red envelope, no more than two decimal places.")

money=round(float(input()),2)
print("Please enter the number of red envelopes.")
number=int(input())
print("Please enter the red envelope :1. Lucky red envelope 2. Ordinary red envelope 3.)
mode=int(input())
money=int(money*100)
if mode == 1:
    for i in range(1, number):
        a = random.randint(1, money-(number-i))  # Randomly generate red envelope amount
        money = money-a
        print("The first" + str(i) + "Personal, copy that." + str(a/100) + "Yuan.")
    print("The first" + str(number) + "Personal, copy that." + str(money/100) + "Yuan.")
elif mode == 2:
    for i in range(1, number+1) :# limit number of people
        print("The first" + str(i) + "Personal, copy that." + str(round((money/100)/number,2)) + "Yuan")
else:
    print("Obtained by nominated Person"+str(money/100) +"Yuan")
Copy the code

Did you see that? This is the conditional judgment if… Just… The magic of logic. So, what’s the code for this condition judgment?

conditional

There are three forms of conditional statements in the Python universe, and we’ll start with the simplest one-way statement: if:

One-way judgment: if

One-way judgment: What does if stand for? We know that when handing out red packets, if the exclusive red packets exceed 200 yuan, there will be a reminder that the amount of a single red packet cannot exceed 200 yuan.

You will notice that in the example above, there is an if… Just. So, let’s translate this in code, so we can say if, so let’s run the code below.

# for a single red envelope denomination
money = 201

Condition: If the exclusive mode of a single red envelope more than 200 yuan

if money > 200:

    # Result: the result of "single red envelope can not exceed 200 yuan" is displayed

    print('No more than 200 yuan per red envelope')
Copy the code

So how does this code work?

First, the first line of code assigns the current situation with the assignment operator = : assign the red envelope value 201 to the variable — red envelope value

Second, determine the if condition: if the value of the variable money >200, execute the command on the next line after the colon.

Step 3: Print the result with the print() command: the amount of a single red envelope should not exceed 200 yuan. Therefore, the one-way judgment logic of the if statement can be summarized as follows:

One detail you might notice here is that there are several empty Spaces after the colon: and before the next line in the conditional code, but why?

First of all, in computer language, the technical term for space is indentation. For example, when we write an essay, we should leave two Spaces blank. This is called the first line indentation. With Python, colons and indentation are syntax. It helps Python distinguish between layers of code and understand the logic and sequence of conditional execution. [Note: Indent is four Spaces or a Tab key]

And, in the if conditional language, indentation does not require us to manually press the space bar. When you type “Enter” in English, our development tools (the programs used to write Python code) will automatically indent the next line of code to the right for your convenience.

At this point, the indented content (the print() function) and the if condition form a code block (the whole thing) that becomes the internal command for the if condition.

This means that if the assignment satisfies the if condition, the computer will execute exactly the command inside the if condition (the indented block of code).

So let’s run the following code and see what happens.

# for a single red envelope denomination
money = 201

Condition: If the exclusive mode of a single red envelope more than 200 yuan

if money > 200:

    # Result: the result of "single red envelope can not exceed 200 yuan" is displayed

print('No more than 200 yuan per red envelope')
Copy the code

IndentationError: Expected an indented block

Indent error: expect an indent block

This is because, when we remove the indentation, the if condition and the print command become two different code groups that are parallel. You see: under the if condition, there is no action that can be performed. Let’s think about it: whether the condition is true or not, nothing is done, and the existence of the condition means nothing.