Python novice tutorial 3, judgment statement

3.1 if statement

if is the meaning of if. Did you discover it? The logic of conditional judgment can all be rewritten with the words "if we do, then we do ."

I believe everyone is familiar with the conditional judgment statements in Scratch, and the Python conditional judgment if statement is similar.

For example:

if monkey_age < 16:
    print('我是未成年')

The meaning of this code is to determine whether the monkey's age is less than 16 years old. If the judgment is established, the [print] statement will be executed.

Similar to Scratch, we can add an else after the if statement. When the condition is not met, the program will execute the content in the else. For example:

if monkey_age < 16:
    print('我是未成年')
else:
    print('我成年了')

 There is a simpler way of writing in Python. We can add elif after the if statement, elif is shorthand for else if, and after elif, add the condition to be judged, then multiple condition judgments can be realized.

like this:

if air_quality < 51:
    print('空气质量优')
else air_quality < 101:
    print('空气质量良')
elif
    print('空气质量差')

3.2 Small test

a = 40
b = 20
c = a - b

if c < 0:
    d = 1
elif c < 10:
    d = 2
elif c < 30:
    d = 3
else:
    d = 4
print(d)

Run this code, what numbers will be printed in the terminal area?

A 1                          B 2                         C 3                         D 4

The correct answer will be announced in the next issue

Answer from the previous issue: Correct order: d = c + a + b

Guess you like

Origin blog.csdn.net/m0_52519239/article/details/111414049