Conditional judgment
The reason why a computer can do many automated tasks is because it can make conditional judgments on its own.
For example, enter the user's age, and print different content according to the age. In the Python program, use the if
statement to achieve:
age = 20
if age >= 18:
print('your age is', age)
print('adult')
According to Python's indentation rules, if the if
statement is judged to be true True
, the indented two-line print statement is executed, otherwise, nothing is done.
You can also if
add a else
statement, which means that if if
the judgment is yes False
, do not execute if
the content, and else
execute it:
age = 3
if age >= 18:
print('your age is', age)
print('adult')
else:
print('your age is', age)
print('teenager')
Be careful not to omit the colon :
.
Of course, the above judgment is very rough, and it can be used elif
for a more detailed judgment:
age = 3
if age >= 18:
print('adult')
elif age >= 6:
print('teenager')
else:
print('kid')
elif
Yes else if
, there can be more than one abbreviation, elif
so if
the full form of the statement is:
if <条件判断1>:
<执行1>
elif <条件判断2>:
<执行2>
elif <条件判断3>:
<执行3>
else:
<执行4>
if
Statement execution has a characteristic. It is judged from top to bottom. If it is in a certain judgment, after the statement corresponding to the judgment is executed, the remaining sum True
is ignored :elif
else
age = 20
if age >= 6:
print('teenager')
elif age >= 18:
print('adult')
else:
print('kid')
if
Judgment conditions can also be abbreviated, such as writing:
if x:
print('True')
As long as it x
is a non-zero value, a non-empty string, a non-empty list, etc., it is judged as True
, otherwise it is False
.
input
By input()
reading the user's input, you can enter your own input, and the program runs more interestingly:
birth = input('birth: ')
if birth < 2000:
print('00前')
else:
print('00后')
Input 1982
, the result is an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()
This is because input()
the returned data type str
can str
not be directly compared to an integer, and must be str
converted to an integer first. Python provides int()
functions to do this:
s = input('birth: ')
birth = int(s)
if birth < 2000:
print('00前')
else:
print('00后')
Run it again to get the correct result. But what if the input abc
? Again you get an error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'
It turns out that int()
when the function finds that a string is not a valid number, it will report an error and the program will exit.
Code:
print('------------------------------------------------------') age = 3 if age >= 18: print('your age is',age) print('adult') elif age >= 6: print('teenager') else: print('kid') print('------------------------------------------------------') s = input('birth:\n') birth = int(s) if birth < 2000: print('before 00') else: print('after 00') print('------------------------------------------------------') x = input('please enter ...\n') if x: print('True') else: print('False')
TestCode:
# -*- coding:utf-8 -*- ''' Xiao Ming is 1.75 in height and 80.5 kg in weight. Please help Xiaoming calculate his BMI index according to the BMI formula (weight divided by the square of height), and according to the BMI index: Below 18.5: too light 18.5-25: Normal 25-28: Overweight 28-32: Obesity Above 32: Severely obese height = 1.75 weight = 80.5 ''' height = input('Please input height:') weight = input('Please enter your weight:') h = float(height) w = float(weight) BIM = w / (h * h) print('Your BIM value is %.2f'%BIM,'\nbelongs to:') if BIM < 18.5: print('too light') elif 18.5 < BIM < 25: print('normal') elif 25 < BIM < 28: print('too heavy') elif 28 < BIM < 32: print('fat') elif BIM > 32: print('Severely obese')