Python conditional basis ---

A condition judging

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, print different content based on age, in a Python program, with the ifstatement to achieve:

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

 

Python indentation according to the rules, if ifthe statement judge True, put the two lines indented print statement is executed, otherwise, do nothing.

Also to ifadd a elsesentence, meaning that if the ifjudgment is False, do not perform ifthe content, to the elseimplementation of:

age = 3
if age >= 18:
    print('your age is', age)
    print('adult')
else:
    print('your age is', age)
    print('teenager')

 

Be careful not to write less colon :.

Of course, the above determination is very rough, you can use elifto make a more detailed determination:

 

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

 

elifIt is the else ifabbreviation can have more elif, so ifthe full form of the statement is:

 

if <条件判断1>:
    <执行1>
elif <条件判断2>: <执行2> elif <条件判断3>: <执行3> else: <执行4>

 

ifThere is a characteristic statement is executed, it is down from the judge, if a judge is on True, the corresponding statement after the judgment executed, ignore the rest elifand else, so, please explain why the test and the following program print It is teenager:

age = 20
if age >= 6:
    print('teenager')
elif age >= 18:
    print('adult')
else:
    print('kid')

ifJudging condition can also be abbreviated, such as writing:

if x:
    print('True')

As long as xa non-zero value, non-empty string, other non-empty list, it is determined Trueotherwise False.

 

Second, the combination of input

Finally, look at a problem evaluated. Many students will use input()to read user input, so you can enter your own program to run more interesting:

birth = input('birth: ')
if birth < 2000:
    print('00前')
else:
    print('00后')

Enter 1982, the results being given:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()

This is because input()the data type is returned str, strnot an integer, and comparison must first be directly strconverted to an integer. Python provides a int()function to do this thing:

string = input('birth: ')
birth = int(string)
if birth < 2000:
    print('00前')
else:
    print('00后')

Run again, you can get correct results. However, if you enter abcit? You will get an error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'abc' 

The original int()error will be found when a string is not a legitimate function of the number, the program exits.

 

III Summary

 

Conditional can let the computer make their own choice, Python's if ... elif ... else very flexible.

Matching condition is determined from the top down, the corresponding block statement is executed when the condition is satisfied, and the subsequent elif else are no longer performed.

Guess you like

Origin www.cnblogs.com/Tomorrow-will-be-better/p/11141698.html