Python 利用字典实现类似 java switch case 功能

def add():
    print('add')

def sub():
    print('sub')

def exit():
    print('exit')

choice = { '1' : add, '2' : sub, '3' : exit}

item = input('please input your number! ')

if item in choice:
    choice[item]()

运行结果:
please input your number! 2
sub
posted @ 2018-05-07 14:12  klvchen  阅读(658)  评论(0编辑  收藏  举报