1.通用格式python
if condition1: statement1 elif condition2: statement2 else: statement3
2.例子:.net
>>> if 1 : print(True) True >>> if 2>0: print(True) True
>>> if 2<0:print(False) elif 1<2<3 :print(True) True >>>
>>> person='killer' >>> if person=='police':print('hi police') elif person=='teacher':print('morning sir') elif person=='killer':print('run away') run away >>>
>>> person='teacher' >>> if person=='police':print('hi police') elif person=='teacher':print('morning sir') elif person=='killer':print('run away') morning sir
>>> person='police' >>> if person=='police':print('hi police') elif person=='teacher':print('morning sir') elif person=='killer':print('run away') hi police >>>
注意:python里面是没有switch...case...这个语法,全部分支都须要写成if...elif...else...code
咱们在演示另外一种多路分支,主要使用在字典里面blog
>>> choice='a' >>> print({'a':1,'b':2,'c':3}[choice]) 1 >>>
>>> choice='a' >>> if choice=='a':print(1) elif choice=='b':print(2) elif choice=='c':print(3) 1 >>>
就说到这里,谢谢你们
get
------------------------------------------------------------------it
版权声明:本文为博主原创文章,未经博主容许不得转载。class