|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Exception :异常
Assert: 断言:当Assert 后面条件为假时,自动抛出一个异常,为真则继续代码
- >>> my_list = ['小甲鱼是帅哥']
- >>> assert len(my_list) > 0
- >>> my_list.pop()
- '小甲鱼是帅哥'
- >>> assert len(my_list) > 0 # assert 后面条件为假,抛出AssertionError
- Traceback (most recent call last):
- File "<pyshell#21>", line 1, in <module>
- assert len(my_list) > 0
- AssertionError
复制代码
- >>> my_list.fishc # 列表没有fishc方法
- Traceback (most recent call last):
- File "<pyshell#22>", line 1, in <module>
- my_list.fishc
- AttributeError: 'list' object has no attribute 'fishc'
复制代码
- >>> my_list = [1,2,3]
- >>> my_list[3] # 此例索引没有3
- Traceback (most recent call last):
- File "<pyshell#24>", line 1, in <module>
- my_list[3]
- IndexError: list index out of range
- >>> my_list[2]
- 3
复制代码
- >>> my_dict = {'one':1,'two':2,'three':3}
- >>> my_dict['one']
- 1
- >>> my_dict['four'] # 此例,关键字没有four
- Traceback (most recent call last):
- File "<pyshell#28>", line 1, in <module>
- my_dict['four']
- KeyError: 'four'
- >>> my_dict.get('four') # 用get方法取没有的键不会报错
复制代码- >>> fishc # 没有fishc这个变量
- Traceback (most recent call last):
- File "<pyshell#30>", line 1, in <module>
- fishc
- NameError: name 'fishc' is not defined
复制代码
- >>> print 'I love FishC.com' # 语法错误
- SyntaxError: invalid syntax (<pyshell#31>, line 1)
复制代码- >>> 1 + '1' # 类型错误
- Traceback (most recent call last):
- File "<pyshell#32>", line 1, in <module>
- 1 + '1'
- TypeError: unsupported operand type(s) for +: 'int' and 'str'
复制代码
- >>> 5 / 0 # 除数为0错误
- Traceback (most recent call last):
- File "<pyshell#33>", line 1, in <module>
- 5 / 0
- ZeroDivisionError: int division or modulo by zero
复制代码
>>>
|
评分
-
查看全部评分
|