python 条件语句

 

例:

  如果 1=1,那么就会输出  "hello world"   否则  输出 "hello penphy"

代码块:

1 if 条件 :
2   print"hello world")
3 else:
4   print ("Error")

  PS:if下面的缩进 也就是空格,代表if 下面的代码块。   (缩进不正确,将会报错)

1 if 1 == 1 :
2     print"hello world")
3 else:
4     print ("hello penphy")

  1=1 条件成立,所以执行第二行的代码 否则 条件不成立 会执行第四行的代码

1 if 1 == 1
2     if 2 == 2
3         print("hello world")
4         print("hello seattle")
5     else:
6         print("hello pengfei")
7 else:
8     print("hello penphy")

  

 

                                                              以上两张图, if else 都是可以嵌套的.

  

 1 inp = input('请输入会员名称:')
 2 if inp == "高级会员"
 3     print('美女')
 4 elif inp == "白金会员"
 5     print('跑车')
 6 elif inp == "铂金会员"
 7     print('一线女演员')
 8 else:
 9     print('城管')
10 
11 print('服务开始吧...')

      ---根据上面的条件,如果不是输入这些会员字眼,会自动输出 城管

总结:

  1. if 基本语句

    if 条件

      内部代码块

      内部代码块

    else:

    ... #内部代码块#

    print('....')

    满足if执行条件时 再往下执行

2. if支持嵌套

3 elif语句

 额外补充:pass语句

      pass

      if 1 ==1:

          pass

        else:

          print('get out')

      PS:

        pass 代指空代码,无意义,仅仅用于表示代码块    

    

            

            

坎坷困难会让你不断的强大起来 -- 前提是你别怂
原文地址:https://www.cnblogs.com/penphy/p/9050544.html