Python的学习之旅———流程控制之if...else....

if...else....

                          

如上面的流程表示 if符合条件(条件是true)就执行条件代码,如果条件为false,则执行eles的代码.

当然 if....eles是可以嵌套的.

Age=int(input("请输入你年龄:"))
Sex=input('请输入你的性别:')
hello = "你好美女"
if Age < 30 and Sex=="女":
print(hello)
elif Age>=30 and Sex=="女":
print('你好阿姨')
else:
print('你好老哥')

Python中if语句的一般形式如下所示:

if c_1: s_b_1 elif c_2: s_b_2 else: s_b_3
  • 如果 "c_1" 为 True 将执行 "s_b_1" 块语句
  • 如果 "c_1" 为False,将判断 "c_2"
  • 如果"c_2" 为 True 将执行 "s_b_2" 块语句
  • 如果 "c_2" 为False,将执行"s_b_3"块语句

Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else

posted @ 2017-09-07 13:33  恩是的  阅读(186)  评论(0编辑  收藏  举报