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!
).如果try代码块语句执行发生异常,Python就跳回try,执行第一个匹配该异常的except块中的代码2).如果异常发生在try代码块内,没有匹配的except子句,name异常就会向上传递到程序中之前进入的try中,或者转到这个进程的顶层(这会使python终止当前程序并打印默认的错误消息)
3).如果try块中的代码执行没有异常发生,则python将执行else块中的代码
3.使用try...finally捕捉异常
当try块中包含了finally语句,则python一定会在执行完try块中的代码之后在执行finally块中的代码(无论try块中的代码是否发生异常都将执行finally块中的代码)
python
用Python条件语句
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
if 判断条件1:
执行语句1……elif 判断条件2:
执行语句2……elif 判断条件3:
执行语句3……else:
执行语句4……
num = 5if num == 3: # 判断num的值
print 'boss'
elif num == 2:
print 'user'
elif num == 1:
print 'worker'
elif num < 0: # 值小于零时输出
print 'error'
else: print 'roadman' # 条件均不成立时输出