网站首页 > 技术文章 正文
一个小错误可能会导致程序不能运行,这对于初学者程序员来说可能非常令人沮丧。本文将介绍初学者在使用Python可能遇到的常见错误和异常。熟悉这些可以帮助你快速了解问题并解决问题。
名称错误
当在Python中未定义对象时,你会收到一个 NameError。
变量使用之前,需要定义,防止出现名称错误。
语法错误
当你在Python中输入不完整时,你会收到一个SyntaxError,比如在定义字符串时,缺少了引号或错误的使用了中文引号。
初学者在输入汉字后,经常会出现忘记关闭输入法,错误的输入中文标点符号。
类型错误
将函数应用于错误类型的数据时,将出现 TypeError。例如将算术运算应用于字符串。
索引错误
当访问超出数据限制的索引时,会收到 IndexError。
键值错误
如果尝试访问字典中未包含的键,则会收到 KeyError。
属性错误
当使用不适用于正在处理的特定数据的属性或方法时,会发生AttributeError。例如,对数值使用lower()方法。
值错误
当将函数应用于不适合该操作的数据类型时,会发生值错误。例如,可以将int()应用于整数字符串,例如:int("9999"),但不能将字母转换为整数,int("Hello World!")。
缩进错误
新手经常犯的另一个错误是缩进错误,这个错误在某种程度上是Python独有的,因为Python使用缩进来定义结构。
作为初学者,需要掌握的关键技能是看懂错误信息,错误信息中,通常会告诉你具体的位置,你会很快改正一些常见错误,这对于初学者是很有用的。
以下是错误示例,供参考。
>>> str = “Hello World!”
SyntaxError: unexpected indent
>>> str1="Hello World!"
>>> print(str2)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
print(str2)
NameError: name 'str2' is not defined
>>> str1="Hello World!
SyntaxError: EOL while scanning string literal
>>> str1="Hello World!”
SyntaxError: EOL while scanning string literal
>>> str1="Hello World!"
>>> print(str1+100)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print(str1+100)
TypeError: can only concatenate str (not "int") to str
>>> str1="Hello World!"
>>> print(str1[20])
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
print(str1[20])
IndexError: string index out of range
>>> dict1={"name":"zhangsan","age":20}
>>> print(dict1["year"])
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
print(dict1["year"])
KeyError: 'year'
>>> n=123
>>> n.lower()
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
n.lower()
AttributeError: 'int' object has no attribute 'lower'
>>> str1="Hello World!"
>>> str2="123"
>>> int(str2)
123
>>> int(str1)
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
int(str1)
ValueError: invalid literal for int() with base 10: 'Hello World!'
>>> def f(n):
if n>=0:
print(n)
else:
print(-n)
SyntaxError: expected an indented block
感谢您的阅读,关注我,精彩继续!
猜你喜欢
- 2025-07-09 Python数据可视化:箱线图多种库画法
- 2025-07-09 春节回家!车位难求啊!看我用Python自动寻找空车位!
- 2025-07-09 Python修饰器,终极解释来了!(python修改器)
- 2025-07-09 Python微信防撤回,基于itchat模块
- 2025-07-09 用Python写爬虫软件的思路(招聘网站为例)
- 2025-07-09 字节跳动竟然斥巨资开发出《Python知识手册》,高清PDF
- 2025-07-09 《菜鸟的python笔记》python初学者的福音,完整版开放下载
- 2025-07-09 字节大佬编写《菜鸟的python笔记》python初学者的福音,免费分享
- 2025-07-09 10个非常适合菜鸟练手的Python项目,墙裂建议收藏
- 276℃Python短文,Python中的嵌套条件语句(六)
- 276℃python笔记:for循环嵌套。end=""的作用,图形打印
- 273℃PythonNet:实现Python与.Net代码相互调用!
- 267℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 267℃Python实现字符串小写转大写并写入文件
- 126℃原来2025是完美的平方年,一起探索六种平方的算吧
- 110℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 107℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)