[5-1] Python entry condition determining if, if-else, if-elif-else

1. if statement

The reason why a computer can do a lot of automated tasks, because it can make their own judgment conditions.

For example, enter the user's age, different content depending on the age of print, in Python programs can be achieved with an if statement:

age = 20
if age >= 18:
    print 'your age is', age
    print 'adult'
print 'END'

Note: indentation rules Python code. The code is regarded as having the same indented code block , rows 3 and 4 above print statement constitutes a code block (but excluding the first print line 5). If the if statement is True, it will execute the code block.

Indentation in strict accordance with the habit of writing Python: 4 spaces, do not use the Tab, Tab not to mix and spaces, or is likely to cause a syntax error because the indentation caused.

Note: if statement then expression, then: represents a code block starts. (colon)

If you knock the code in the Python interactive environment, but also pay special attention to indent, and the need to exit the indentation knock line Enter:

>>> age = 20
>>> if age >= 18:
...     print 'your age is', age
...     print 'adult'
...
your age is 20
adult

Task:
If you score 60 points or more is considered passed.

Bart assume scores of students is 75, please use the if statement to determine whether or not to print out passed:

From writing code:

score = 75
if score>=60:
    print 'passed'

Here Insert Picture Description

2. if-else statements

When the result of the if statement determines the expression is True, the block of code will be included if:

Age IF> = 18:
Print 'Adult'
If we want to judge the age of 18 years of age, print out the 'teenager', how do?

The method is a write if:

if age < 18:
    print 'teenager'

Or not by calculating:

if not age >= 18:
    print 'teenager'

Observant students may find that these two conditions are judged "either-or", or meet the conditions 1, 2 or eligible, therefore, can use an if ... else ... statements unite them:

if age >= 18:
    print 'adult'
else:
    print 'teenager'

Use if ... else ... statements, we can True or False condition expression, if performed separately or else block code block.

Note: There is a back else ":." (colon)

Task:
If you score 60 points or more is considered passed, or as failed.

Bart assume scores of students is 55, please print out the if statement passed or failed:

From writing code:

score = 55
if score>=60:
    print 'passed'
else:
    print 'failed'

Here Insert Picture Description

3. if-elif-else statements

Sometimes an if ... else ... is not enough. For example, divided according to age:

Conditions 1:18 or older: adult
Condition 2: 6 years and older: teenager
Condition 3: 6 years of age: kid

We can use a if age> = 18 determines whether the condition 1, if not, then determine age> = 6 to determine whether the conditions through a 2 if, otherwise execution condition 3:

if age >= 18:
    print 'adult'
else:
    if age >= 6:
        print 'teenager'
    else:
        print 'kid'

Write out, we get a ** two nested if ... else ... statement. ** This logic is no problem, but if the condition continues to increase, such as 3 years of age are baby:

if age >= 18:
    print 'adult'
else:
    if age >= 6:
        print 'teenager'
    else:
        if age >= 3:
            print 'kid'
        else:
            print 'baby'

This will only increase the indent, the code will be more ugly.

To avoid nested structure if ... else ..., we can use if ... elif ... else ... more structure, once finished all the rules:

if age >= 18:
    print 'adult'
elif age >= 6:
    print 'teenager'
elif age >= 3:
    print 'kid'
else:
    print 'baby'

elif meaning is else if. As a result, we wrote structure is very clear set of conditions to determine.

Special Note: This series will determine the condition from top to bottom to determine if a judge is True, executing the corresponding code block, conditional on the back of directly ignored, no longer carried out.

Consider the following code:

age = 8
if age >= 6:
    print 'teenager'
elif age >= 18:
    print 'adult'
else:
    print 'kid'

When age = 8, the result is correct, but age = 20, why not print out adult?

If you want to repair, how to repair?

Task:
If the score delineated in accordance with the results:

90分或以上:excellent

80分或以上:good

60分或以上:passed

60分以下:failed

Please write a program to print the results based on score.

From writing code:

score = 85

if score>=90:
    print 'excellent'
elif score>=80:
    print 'good'
elif score>=60:
    print 'passed'
else:
    print 'failed'

Here Insert Picture Description

Published 20 original articles · won praise 0 · Views 415

Guess you like

Origin blog.csdn.net/yipyuenkay/article/details/103892367