2).如果异常发生在try代码块内,没有匹配的except子句,name异常就会向上传递到程序中之前进入的try中,或者转到这个进程的顶层(这会使python终止当前程序并打印默认的错误消息)
3).如果try块中的代码执行没有异常发生,则python将执行else块中的代码
3.使用try...finally捕捉异常
当try块中包含了finally语句,则python一定会在执行完try块中的代码之后在执行finally块中的代码(无论try块中的代码是否发生异常都将执行finally块中的代码)
python while循环语句是:while 判断条件(condition);执行语句(statements)。
执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
当判断条件假 false 时,循环结束。
实例:
#!/usr/bin/python
count = 0
while (count <9):
print 'The count is:', count
count = count + 1
print "Good bye!"
运行实例 »
以上代码执行输出结果:
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!