CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day11/ex1.py
Views: 729
1
"""
2
异常机制 - 处理程序在运行时可能发生的状态
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-13
7
"""
8
9
input_again = True
10
while input_again:
11
try:
12
a = int(input('a = '))
13
b = int(input('b = '))
14
print('%d / %d = %f' % (a, b, a / b))
15
input_again = False
16
except ValueError:
17
print('请输入整数')
18
except ZeroDivisionError:
19
print('除数不能为0')
20
# 处理异常让代码不因异常而崩溃是一方面
21
# 更重要的是可以通过对异常的处理让代码从异常中恢复过来
22
23