Function objects and closure operations

1, the function object code is optimized if the multibranched Lianshu

DEF log_in ():
     Print ( ' login function ' ) 

DEF the Register ():
     Print ( ' registration function ' ) 

DEF check_balance ():
     Print ( ' check balance ' ) 

DEF the withdraw ():
     Print ( ' withdrawal ' ) 

DEF Transfer ( ):
     Print ( ' transfer ' ) 

DIC = {
     ' 0 ' : { ' exit ', Exit},
     ' . 1 ' : { ' register ' , Register},
     ' 2 ' : { ' login ' , log_in},
     ' . 3 " : { " balance inquiry " , check_balance},
     ' . 4 ' : { ' withdrawal ' , } the withdraw,
     ' . 5 ' : { ' transfer ' , Transfer} 
} 
the while True:
     for K in DIC:
         Print  (K,I say [k] [0])
    cmdThe INPUT = ( ' Please enter the command number: ' ) .strip ()
     IF  not cmd.isdigit ():
         Print ( ' you must enter the number of digital commands, silly fork ' )
         the Continue 
    IF cmd == ' 0 ' :
         BREAK 
    IF cmd in DIC: 
        DIC [cmd] [ . 1 ] (name)
     the else :
         Print ( ' no command ' )

 

 

Calculator accumulate

def f1():
    x=0
    def counter():
        nonlocal x
        x += 1
        return x
    return counter
f2 = f1()
print(f2())
print(f2())
print(f2())
print(f2())
print(f2())

 

Guess you like

Origin www.cnblogs.com/bk134/p/12533822.html