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 if
statement to achieve:
age = 20 if age >= 18: print('your age is', age) print('adult')
Python indentation according to the rules, if if
the statement judge True
, put the two lines indented print statement is executed, otherwise, do nothing.
Also to if
add a else
sentence, meaning that if the if
judgment is False
, do not perform if
the content, to the else
implementation 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 elif
to make a more detailed determination:
age = 3 if age >= 18: print('adult') elif age >= 6: print('teenager') else: print('kid')
elif
It is the else if
abbreviation can have more elif
, so if
the full form of the statement is:
if <条件判断1>: <执行1> elif <条件判断2>: <执行2> elif <条件判断3>: <执行3> else: <执行4>
if
There 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 elif
and 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')
if
Judging condition can also be abbreviated, such as writing:
if x: print('True')
As long as x
a non-zero value, non-empty string, other non-empty list, it is determined True
otherwise 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
, str
not an integer, and comparison must first be directly str
converted 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 abc
it? 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.