2018.09.14python学习第四天part1

流程控制之if判断

1.什么是if判断?(what)

判断一个if条件,如果成立则做什么,如果不成立则做什么

2.为什么要有if判断?(why)

为了让电脑像人一样有判断能力。(赋予它逻辑判断)

3.如何使用if判断?(how)

#语法一:

 if条件1:

  code1

  code2

  ........

age=18
if age==18:
    print("hello i love u")

#语法二:

if条件:

  code1

  code2

  .......

else:

  code1

  code2

  ..........

age=18
sex="female"
is_beautiful=True

if age>16 and age<30 and sex=="female" and is_beautiful:
    print("i love u)
else:
    print("阿姨你好")

#语法三:(if的嵌套)

if条件1:

  if条件2:

    code1

    code2

    .....

  code2

  code3

  .......

age=18
sex=female
is_beautiful=True
is_successful=True

if age>16 and age<30 and sex==female and is_beautiful:
    print("开始表白")
    if is_successful:
        print("在一起")
    else:
        print("我有钱")
else:
    print("阿姨再见")

#语法四:

if 条件1:

  code1

  code2

elif 条件2:

  code3

  code4

elif 条件3:

  code5

  code6

..........

else:

  code7

  code8

score = input(" your score")
score=int(score)
if score>90:
    print("very good")
elif score<=80:
    print("good")
elif score<=70:
    print("just so so")
else:
    print("bad")

 

ps:  1.注意if条件结束以后一定要用:

  2.如果一行代码太长的话可以用/分隔后另起一行

  3.同一子代码块中的代码缩进应该相同

 

posted on 2018-09-14 15:31  撩与诗人  阅读(111)  评论(0编辑  收藏  举报