趣学PYTHON第5章if else

趣学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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值