###############错误和异常########################
说明:e 是错误的具体原因,else 表示没有异常才会执行else的语句,finally 是无乱有没异常都要执行
raise 自定义触发异常
assert 断言异常,assert 后面的为真,这句话没有意义,不会执行;为假,则会执行‘Age out of range’
############例子###############
#############with 语句###################
######################函数的参数####################
#####‘*args'#########
In [9]: def foo(*args): ...: print args ...: In [10]: foo(10) (10,) In [11]: foo(10,20,'fush') (10, 20, 'fush')
###########################
In [12]: def add(x,y):
....: return x+y
....:
In [13]: add(*[10,20])
Out[13]: 30
In [14]: add(*(10,20))
Out[14]: 30
###########**args#############
In [15]: def bar(**args): ....: print agrs In [18]: bar(name='bob',age=23) {'age': 23, 'name': 'bob'}
##########大招################
In [21]: def fun1(args,*non_args,**kwargs): print args print non_args print kwargs In [23]: fun1(10) 10 () {} In [24]: fun1(10,20) 10 (20,) {} In [25]: fun1(10,(20,30)) 10 ((20, 30),) {} In [26]: fun1(10,(20,30),name='bob') 10 ((20, 30),) {'name': 'bob'}
######################实战案例################
[root@master script]# cat num_game.py #!/usr/bin/python # coding:utf-8 import random def probe(): CMDs = {'+':add,'-':sub} N_list = [random.randint(1,50) for i in range(2)] N_list.sort(reverse=True) op = random.choice('+-') answer = CMDs[op](*N_list) prompt = '%s %s %s = ' % (N_list[0],op,N_list[1]) tries = 0 while tries < 3: result = int(raw_input(prompt)) if answer == result: print 'Very Good!' break print 'answer is wrong!' tries +=1 else: print '