VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python异常大总结(4)

晕死~! 怎么办呢?总有被我们不小心忽略处理的情况,如果真想用一段代码捕捉所有异常,那么可在except子句中忽略所有的异常类:

1
2
3
4
5
6
7
8
9
try:
x= input('Enter the first number: ')
y= input('Enter the second number: ')
print x/y
except:
print '有错误发生了!'
#再来输入一些内容看看
>>>
Enter the first number:'hello' * )0

有错误发生了!

结束

别急!再来说说最后一个情况,好吧,用户不小心输入了错误的信息,能不能再给次机会输入?我们可以加个循环,保你输对时才结束:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while True:
try:
x= input('Enter the first number: ')
y= input('Enter the second number: ')
value= x/y
print 'x/y is',value
except:
print '列效输入,再来一次!'
#运行
>>>
Enter the first number:10
Enter the second number:
列效输入,再来一次!
Enter the first number:10
Enter the second number:'hello'
列效输入,再来一次!
Enter the first number:10
Enter the second number:2
x/yis 5

 



相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号