- 博客(5)
- 收藏
- 关注
原创 Python 中的 NameError: name ‘x‘ is not defined 错误
NameError: name 'x' is not defined` 是 Python 中常见的错误之一,表示你尝试使用了一个尚未定义或不在当前作用域内的变量。print("全局变量:", list(globals().keys()))print("局部变量:", list(locals().keys()))### 1. 使用 `locals()` 和 `globals()` 检查变量。**解决方法:** 使用 `nonlocal` 关键字。**解决方法:** 使用 `global` 关键字。
2025-10-31 15:58:52
322
原创 Python 中的 TypeError: unsupported operand type(s) for +: ‘int‘ and ‘str‘ 错误
info = "{}今年{}岁,考试成绩是{}分".format(name, age, score)info = "%s今年%d岁,考试成绩是%.1f分" % (name, age, score)print("我今年", age, "岁") # 输出: 我今年 25 岁。info = f"{name}今年{age}岁,考试成绩是{score}分"message = "我今年" + str(age) + "岁"message = "我今年{}岁".format(age)
2025-10-31 15:58:18
420
原创 Python 中的 SyntaxError: invalid syntax 错误
【代码】Python 中的 SyntaxError: invalid syntax 错误。
2025-10-31 15:57:45
550
原创 Python 中的 NameError: name ‘x‘ is not defined 错误
错误示例x = 10# 正确示例x = 10print(x) # 输出: 10。
2025-10-31 15:57:02
406
原创 Python 中的 SyntaxError: invalid syntax 错误
错误示例 - 函数参数默认值使用可变对象def func(items=[]): # 这会有逻辑问题,但不是语法错误# 错误示例 - 函数调用参数格式错误使用正确的函数定义和调用语法。# 更好的做法 - 使用 None 作为默认值items = []# 函数调用。
2025-10-31 15:56:07
325
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅