09-11-周四_14-09-47

This commit is contained in:
2025-09-11 14:09:47 +08:00
parent 041e54c8c8
commit 19a43d9e1d

View File

@@ -10,12 +10,24 @@
#语法错误示范一
if
if
^
SyntaxError: invalid syntax
#语法错误示范二
def test:
pass
def test:
^
SyntaxError: expected '('
#语法错误示范三
print(haha
print("hello"
print("hello"
^
SyntaxError: '(' was never closed
```
### 逻辑错误
@@ -25,9 +37,22 @@ print(haha
num=input(">>: ")
res1 = int(num)
ret = int(num)
^^^^^^^^
ValueError: invalid literal for int() with base 10: 'abc'
#无法完成计算
res1=1/0
a = 1/0
~^~
ZeroDivisionError: division by zero
res2=1+'str'
a = "hello" + 1
~~~~~~~~^~~
TypeError: can only concatenate str (not "int") to str
```
### 异常
@@ -45,6 +70,7 @@ res2=1+'str'
# 触发IndexError
l=['eagle','aa']
l[3]
# 触发KeyError
dic={'name':'eagle'}
dic['age']
@@ -56,8 +82,9 @@ int(s)
常见异常
| AttributeError | 试图访问一个对象没有的属性比如foo.x但是foo没有属性x |
| 异常 | 描述 |
| :---------------- | :----------------------------------------------------------- |
| AttributeError | 试图访问一个对象没有的属性比如foo.x但是foo没有属性x |
| IOError | 输入/输出异常;基本上是无法打开文件 |
| ImportError | 无法引入模块或包;基本上是路径问题或名称错误 |
| IndentationError | 语法错误(的子类) ;代码没有正确对齐 |
@@ -317,7 +344,7 @@ except EvaException as e:
### 断言
表达式True时程序继续运行表达式为False时程序终止运行并报AssertionError错误
表达式True时程序继续运行表达式为False时程序终止运行并报AssertionError错误
```python
assert 1 == 1