[Python] Judgment of З basic structures

🚩write in front🚩

🔎Hello everyone, my name is Ze Ze, I hope that after reading it, it can be helpful to you, please correct me if there is any inadequacy! Learn and communicate together 🔎
🏅 2021 Blog Star IoT and Embedded Development TOP5~2021 Blog Star Top 100~Alibaba Cloud Expert & Alibaba Cloud Star Blogger~Nuggets⇿InfoQ Creator~Weekly List 99»Total List 1184🏅
🆔This article was first published by Keze’s original CSDN 🙉If you need to reprint, please notify
 ⚠ 📝 Personal homepage – [Keze’s blog_CSDN blog] 📃
🎁Welcome everyone → like👍 + favorite ⭐️ + message 📝
✉️ We are not on the stage we choose, the performance is not the script we choose📩 

【Python】”Judgment” directory of З basic structure👇

🚩write in front🚩

🍻Logic Control

🍻 Digital Logic Condition

🍻 Conditional control

🍻if – usage

🍻if – else usage

🍻if nesting

🍻 Practice Questions → Number Guessing Game

🍻 Description

🍻Logic Control

Logical control is actually judgment, and judgment is very important, no matter what language it is. Without judgment, it is incomplete language. So judgment is very important.

  • Complex algorithms can be implemented using 3 basic structures, including loops, sequences, and judgments .

In the design of the program, the code is not executed step by step in order. When running to a certain line of code, it is necessary to stop and judge which branch code will be run next. This judgment represents the branch structure.

The branch structure can be judged using the if statement, and we are also talking about the if statement in this blog.

In the program, there may be situations that need to be looped. For example, if you enter Python 100 times, then you need to use a loop statement. It is impossible for us to print 100 times directly, which is extremely inefficient. So, obviously not possible. Then we need to use while, for, range and so on.

The logic flow control in Python also includes statements such as jumping out of the break, continuing, and traversing the range. These flow control statements will be described in detail later.

🍻 Digital Logic Condition

Before the official start, let’s talk about the logical conditions commonly used in mathematics, because many of these logical conditions will be used in conjunction with the conditional control if.

Equal to: a == b

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

🍻 Conditional control

Conditional control statements are very common in any programming language.

The flow chart is as follows↓

For example: Let’s say you got up this morning, you studied all day yesterday, and today you want to lie down, but you watch everyone else roll there, and you want to study all day. You are very hesitant at this time, you see a coin in front of you, and you want to toss the dice. The positive number is for learning, and the negative ❀ lies flat. Then in programming, you will use the judgment statement if.

Next, write down the expression of if in the Python statement, as shown in the following code👇

if conditional statement 1:
    execute statement 1
elif conditional statement:
    execute statement 2
else:
    execute statement 3

The execution process is as follows👇

①→ Judge whether the conditional statement 1 is True (true), if it is True, execute the statement block 1, and other statement blocks will not be executed. So the entire conditional structure statement is executed. Then continue to execute other statements, if conditional statement 1 is False (false), then judge whether conditional statement 2 is true.

②→If it is judged that conditional statement 2 is True, execute statement 2, and then execute other statements. If it is false, execute statement 3 directly, and the conditional control statement ends.

🔥Tip → The statement block after the colon and the statement on the line where the colon is located must be indented.

🍻if – usage

The if else statement in Python can be subdivided into three forms, namely if statement, if else statement and if elif else statement. We have already introduced if elif else above. Next, we will introduce if(), which is also the simplest form of judgment.

The flow chart is as follows↓

If the expression holds (true), execute the following block of code; if the expression does not hold (false), execute nothing. Then you can’t just talk and practice, so let’s do a topic practice. Select the structure to determine whether the user meets the conditions, the 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 18 years old, only %d years old.' %num)

print ( 'Welcome!' )

Running result 🖊

Please enter your age: 14
You can’t go to the Internet cafe yet!
Under 18 years old, only 14 years old.
welcome!

The result of running another case.

Please enter your age: 18
Welcome!

Zhang San has doubts at this time, why is he still welcome to enter 14?

Then we have to use if – else or double if to solve this situation.

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 18 years old, 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 18 years old, only 14 years old.

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

  1. Zhang San turns 18.
  2. Zhang San is under the age of 18.

🍻if – else usage

The expression of if – else, as shown in the following code👇

if conditional statement 1:
    execute statement 1
else:
    execute statement 2

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

If the conditional statement is false, statement 2 is executed.

  • If the expression is true, the code block 1 immediately following the if is executed; if the expression does not hold, the code block 2 immediately following the else is executed.

The flow chart is as follows↓ 

The following is an example of inputting a number to determine whether the number is odd or even. As shown in the code below👇 

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 a number…12
12 is even.

Please enter a number…11
11 is odd.

Then choose the structure to determine whether the user meets the conditions, and we use the if else method to solve it. 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 18 years old, 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 18 years old, only 14 years old.

If the conditional statement is true, statement 1 is executed. The contents of the else statement are not executed.

🍻if nesting

In our development program, we will use if to judge conditional statements, so suppose we want to add conditional judgments to the execution statements where the conditions are established. This time you can use the nesting of if statements.

The nested application scenario of if is: when the previous conditions are met, additional judgments are made.

The nested syntax format of if has no difference except indentation. The syntax format is as follows↓

if condition 1:
    Execute the statement when condition 1 is met
    ...
    If condition 1 is based on condition 2:
        Execute the statement when condition 2 is met
        ...
    # Condition 2 is not met 
    else :
        Condition 2 is not met, the code to execute

# If condition 1 is not met 
else :
    Execute the statement if condition 1 is not met

This is actually a small if statement nested in a large if statement. The above one can be understood after reading more, and it can be understood once with language foundation. All the same (@^0^) 

🍻 Practice Questions → Number Guessing Game

The import random() method returns a randomly generated real number. It is recommended to use the content in this to the first line of the starting line.

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

As shown in the code below👇

import random   # Generate random value 
person = int(input( 'Please enter guessing [0 rock, 1 scissors, 2 cloth]:...' ))
computer = random.randint( 0 , 2 )       # rand(); function randomly generates numbers between 0~32767 
# print three cases 
# person wins 
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' )
     pass 
# Lost 
else :
    print( 'Come again if you don't agree' )

Three cases at runtime🖊

Won: Old Iron 666

Tie: definitely next time

Lost: come back if you refuse to accept

🍻 Description

An “expression” can be a single value or variable, or a complex statement consisting of operators, in any form, as long as it can get a value. Regardless of the type of the result of the “expression”, if else can determine whether it is true (true or false).

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

:Don’t forget the colon at the end of if, elif, and else statements . 

Leave a Comment

Your email address will not be published. Required fields are marked *