Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
print(“I love Python.”)
SyntaxError: invalid character '“' (U+201C) (错误使用中文字符—引号)
1/0
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
1/0
ZeroDivisionError: division by zero (语法错误,除数不能为0)
"FishC"+520
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
"FishC"+520
TypeError: can only concatenate str (not "int") to str (字符串加数字错误) 一、 处理异常try——except处理和捕获异常 try: 检测范围 except:[expression[as identifier]]: 异常处理代码
try: (try语句中出现异常,就会执行except语句,而不是抛出红色的输出)
1/0
except:
print("出错了~")
Traceback (most recent call last):
File "<pyshell#14>", line 2, in <module>
520 + "FishC"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
try:
1/0
except ZeroDivisionError as e: (as e 将异常原因提取出来)
print(e)