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
原文地址:https://www.cnblogs.com/klvchen/p/9002348.html