趣学PYTHON第5章if else
if语句的结构和c中不同,
# -*- coding: utf-8 -*-
"""2018-1-23@author: linda"""
age = 23
if age >20:
print('you are old!')
print('what can you do ?')
print('you can do it now,or too late!')
=================== RESTART: C:\Python27\lianxi\2018123.py ===================
you are old!
what can you do ?
you can do it now,or too late!
>>>
当然,如果年龄小于20,是不会打印这些的。
缩进很重要,
像这样,如果三个print的缩进不一致,就报错了
2.if else语句
# -*- coding: utf-8 -*-
"""2018-1-23@author: linda"""
print('what to hear a dirty joke?')
age = 13
if age > 13:
print('shh, it is a secret')
else:
print('a pig fell in the mud!')
=================== RESTART: C:\Python27\lianxi\2018123.py ===================
what to hear a dirty joke?
a pig fell in the mud!
>>>
如果age>13,将会输出不同的结果。
3.if elif
print('your age?')
age = 12
if age > 13:
print('your are old')
elif age ==13:
print('13!')
elif age == 12:
print('12!')
elif age == 11:
print('11!')
=================== RESTART: C:\Python27\lianxi\2018123.py ===================
your age?
12!
4.组合条件
print('your age?')
age = 12
if age > 20 and age < 40:
print('you are old')
elif age >= 40:
print('you are too old ')
elif age >= 12 and age <=20:
print('you are yonger')
elif age <= 11:
print('small baby!')
=================== RESTART: C:\Python27\lianxi\2018123.py ===================
your age?
you are yonger