您的位置:首页 > 编程语言 > Python开发

[python学习手册]if语句

2014-09-01 13:26 459 查看
1、是-否:

if test1:
statement1
else:
statement2



2、是-否-其它:

if test1:
statement1
elif test2:
statement2
elif test3:
statement3
else:
statement4




3、类'switch':

choice='def'
branch={
'abc':1.80,
'def':1.92,
'hij':2.89
}[choice]
其它的方法:

a、print(branch.get('abc','Bad Choice'))

b、try方法

try:
print(branch[choice])
except KeyError:
print('Bad choice')

4、调用类似钩子函数:

def function(): ...
def default(): ...
branch = {'spam': lambda: ...,
'ham':  function,
'eggs': lambda: ...}
branch.get(choice, default)()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: