SoFunction
Updated on 2024-11-19

Python basic structure of the judgment statement usage details

logical control

Logical control is actually judgment, judgment is very important, no matter which language. No judgment that is not a complete language. Therefore, judgment is very important.

Complex algorithms can be implemented using ③ basic structures, which are loops, sequences, and judgments.

In the design of the program, the code is not gradually in accordance with the order of execution, in the operation of a line of code, you need to stop to judge the next branch of the code will be run to that branch of the code, this judgment is represented by the branching structure.

Branching structures can be judged using if statements, and we are talking about if statements in this blog.

In the program may encounter the need to loop appear in the situation, for example, input 100 times Python, then at this point you need to use the loop statement, we can not directly print 100 times print, this is extremely inefficient. This is extremely inefficient. So, obviously you can't. Then we need to use while, for, range and so on.

Python in the logic of flow control also includes jumping out of the breal, continue continue, traversing the range and other statements later will also introduce these flow control statements in detail.

digital logic condition (DLC)

Before we get started, let's talk about the logical conditions that are commonly used in math, since many of them are used in conjunction with the conditional if control.

Equivalent: a == b

is not equal to: a ! = b

Less than: a < b

Less than or equal to: a <= b

Greater than: a > b

Greater than or equal to: a >= b

conditioned control

Conditional control statements are very common in any programming language.

The flowchart is as follows↓

Let's say: let's say you get up this morning, you studied all day yesterday and today you want to lie down flat, but then you look at everyone else rolling there and you want to study all day again. At this point you are very hesitant, you see a coin in front of you and you want to throw the color. Heads numbers are studying, tails ❀ just lay flat. Then in programming you are going to use the judgment statement if.

Next, we'll write down the Python expression for if, as shown in the following code

if Conditional Statement 1.
Execution statement 1
elif Conditional Statements.
Execution statement 2
else:
Execution statement 3

The execution process is as follows

①→Judge whether the conditional statement 1 is Ture (true), if it is Ture, then execute statement block 1, the other statement blocks will not be executed. Therefore, the entire conditional structure statement is executed. If the conditional statement 1 is False, then determine whether the conditional statement 2 is true.

②→If the conditional statement 2 is Ture, execute statement 2, and then execute other statements; if it is false, execute statement 3 directly, and the conditional control statement ends.

Hint → The statement block following the colon HO the statement on the line where the colon is located is to maintain an indentation.

if - Usage

The if else statement in Python can be broken down into three forms: if, if else, and if elif else. We've already covered if elif else above, so let's move on to if(), which is also the simplest form of judgment.

The flowchart is as follows↓

If the expression is valid (true), then the next block of code is executed; if the expression is not valid (false), then nothing is executed. Then you can not just talk without practicing, so let's do a topic to practice. Select the structure to determine whether the user meets the conditions, sample code is as follows ↓

num = int(input('Please enter your age:'))
if num < 18:
    print('You can't go to the internet cafe yet!')
    print('Under the age of 18, only %d years old.'%num)
 
print('Welcome!')

running result

Please enter your age:14
You can't go to the Internet cafe yet!
Under the age of 18, only 14.
Welcome!

Running results for another scenario.

Please enter your age: 18
Welcome!

Zhang San had questions at this point, why would entering 14 still be welcome.

In this case, we'll use if - else or double if.

Double if Statement Code Example ↓

num = int(input('Please enter your age:'))
if num < 18:
    print('You can't go to the internet cafe yet!')
    print('Under the age of 18, only %d years old.'%num)
if num >= 18:
    print('Welcome!')

Please enter your age:14
You can't go to the Internet cafe yet!
Under the age of 18, only 14.

The double if statement is used here because we have two levels of judgment here:

The case of Zhang San turning 18 years old.

The case of Zhang San, who is under 18 years of age.

if - else Usage

The if - else expression is shown in the following code

if Conditional Statement 1.
Execution statement 1
else:
Execution statement 2

The conditional statement is true and statement 1 is executed.

The conditional statement is false and statement 2 is executed.

If the expression holds, execute block 1 immediately after if; if the expression does not hold, execute block 2 immediately after else.

The flowchart is as follows↓

The following is an example of inputting a number to determine whether the number is odd or even. The following code shows that

number = int(input('Please enter a number...'))
if number % 2 == 0:
    print('%d is even'%(number))
else:
    print('%d is odd'%(number))

running result

Please enter the number... .12
12 is an even number.

Please enter the number... .11
11 is an odd number.

Then the next selection structure to determine whether the user meets the conditions we use the if else method to solve. The code example is as follows

num = int(input('Please enter your age:'))
if num < 18:
    print('You can't go to the internet cafe yet!')
    print('Under the age of 18, only %d years old.'%num)
else:
    print('Welcome!')

running result

Please enter your age:14
You can't go to the Internet cafe yet!
Under the age of 18, only 14.

If the conditional statement is true, statement 1 is executed. else is not executed.

if nesting

In our program development, we will use if for conditional statements, so suppose we want to add a conditional judgment in the execution of the statement in which the condition is established. At this point, you can use the if statement nesting.

Nested if's are used in scenarios where an additional judgment is made when the previous condition is satisfied.

There is no difference in the format of the nested syntax of if, except for indentation, which is shown below↓.

if Condition 1.
Satisfying condition 1 executes the statement
    ...
if Condition 2 on the basis of Condition 1.
Satisfying condition 2 executes the statement
        ...
# of cases where condition 2 is not met
    else:
Code to be executed if condition 2 is not fulfilled
 
# of cases where condition 1 is not met
else:
Execution statement if condition 1 is not met

This is actually a big if statement nested in a small if statement, the above this read more to understand, have a basic language once can understand. It's all the same (@^0^).

Practice questions → Guess the number trivia game

The import random() method returns a randomly generated real number. It is recommended that you use this in the first line of the start line.

rand(start number, end number); function randomly generates numbers between 0 and 32767.

As shown in the following code

import random  # Generate random values
person = int(input('Please enter guess[0 rock,1 scissors,2 cloth]:...'))
computer = (0,2)      # rand(); function randomly generate numbers between 0~32767
# Print three scenarios
# The man has won
if person == 0 and computer == 1:
    print('Old Iron 666')
    pass
elif person == 1 and computer == 2:
    print('Old Iron 666')
    pass
elif person == 2 and computer == 0:
    pass
    print('Old Iron 666')
    pass
# Tie
elif person == computer:
    print('Next time for sure')
    pass
# Lost
else:
    print('Come back if you don't want to')

Three scenarios at runtime

Won: Lao Tie 666

Tie: Next time for sure

Lost: Not Again

clarification

An "expression" can be a single value or variable, or a complex statement of operators, in any form as long as it yields a value. If else determines whether an expression is valid (true or false), regardless of the type of result it produces.

A "code block" consists of several statements with the same indentation.

The if, elif, and else statements all have colons at the end: don't forget that.

This article on the use of Python basic structure of the judgment statement is introduced to this article, more related Python judgment statement content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!