#! /usr/bin/python
msgCtrl = "1 : pause\n2 : stop\n3 : restart\nother to quit\n"
ctrlMap = {
'1': lambda : doPause(),
'2': lambda : doStop(),
'3': lambda : doRestart()}
def doPause():
print 'do pause'
def doStop():
print 'do stop'
def doRestart():
print 'do restart'
if __name__ == '__main__':
while True:
print msgCtrl
cmdCtrl = raw_input('Input : ')
if not ctrlMap.has_key(cmdCtrl):break
ctrlMap[cmdCtrl]()
示例二:带参数
复制代码 代码如下:
#! /usr/bin/python
msgCtrl = "1 : +\n2 : -\n3 : *\nother to quit\n"
ctrlMap = {
'1': lambda x,y : x+y,
'2': lambda x,y : x-y,
'3': lambda x,y : x*y}
if __name__ == '__main__':
while True:
print msgCtrl
cmdCtrl = raw_input('Input : ')
if not ctrlMap.has_key(cmdCtrl):break
print ctrlMap[cmdCtrl](10,2),"\n"