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