Conditional judgment
Computers can do a lot of automated tasks, because it can make their own conditions to judge.
For example, enter the age of the user, print different content according to age, in the Python program, with the if
statement implementation:
20if age >= 18: print(‘your age is‘, age) print(‘adult‘)
According to the indentation rules of Python, If the if
statement is judged True
, the indented two lines of the print statement are executed, otherwise, nothing is done.
It is also possible to if
add a else
statement, meaning that if the if
judgment is False
, do not execute if
the content, go to put the else
executed:
3if age >= 18: print(‘your age is‘, age) print(‘adult‘)else: print(‘your age is‘, age) print(‘teenager‘)
Be careful not to write a :
colon less .
Of course, the above judgment is very sketchy, can be used to elif
make a more detailed judgment:
3if age >= 18: print(‘adult‘)elif age >= 6: print(‘teenager‘)else: print(‘kid‘)
elif
is else if
the abbreviation, can have more than one elif
, so if
the complete form of the statement is:
<条件判断1>: <执行1>elif <条件判断2>: <执行2>elif <条件判断3>: <执行3>else: <执行4>
if
statement execution has a characteristic, it is to judge from the top, if in a certain judgment True
, the corresponding statement of the judgment is executed, ignore the remaining elif
and else
, so, please test and explain why the following program printed teenager
:
20if age >= 6: print(‘teenager‘)elif age >= 18: print(‘adult‘)else: print(‘kid‘)
if
The judging conditions can also be abbreviated, such as writing:
if x: print(‘True‘)
As long as a non- x
0 value, a non-empty string, a non-empty list, etc., it is judged True
, otherwise False
.
Further discussion on input
Finally see a problematic condition to judge. Many students will input()
read the user's input, so that they can input, the program runs more interesting:
birth = input(‘birth: ‘)if birth < 2000: print(‘00前‘)else: print(‘00后‘)
Input 1982
, result error:
Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unorderable types: str() > int()
This is because the input()
returned data type is str
str
not directly compared to an integer and must first be str
converted to an integer. Python provides a int()
function to do this thing:
s = input(‘birth: ‘)birth = int(s)if birth < 2000: print(‘00前‘)else: print(‘00后‘)
Run again to get the right results. But what if I enter abc
it? And you get an error message:
call last): File "<stdin>", line 1, in <module>ValueError: invalid literal for int() with base 10: ‘abc‘
The original int()
function found a string is not a valid number when the error occurs, the program exits .
How do I check and capture the program run-time errors? The subsequent errors and debugs will be covered.
Practice
Xiao Ming is 1.75 tall and weighs 80.5kg. Please help Xiao Ming calculate his BMI according to the BMI formula (weight divided by the square of height) and according to the BMI Index:
- Less than 18.5: too light
- 18.5-25: Normal
- 25-28: Overweight
- 28-32: Obesity
- Above 32: Severe obesity
To if-elif
determine and print the result:
Summary
Conditional judgment allows the computer to make its own choices, and Python's if...elif...else is flexible.
Reference source
do_if.py
Python 5 conditional judgment