# Process finished with exit code 0 程序结束后 正常退出 code 为 0
# print('hello world')
# Process finished with exit code 1 程序异常结束 code 为 1
# print(a)
# 常见的bug
# NameError: name 'a' is not defined (一般只变量名错误)
# 如果遇到此类错误,查看变量名是否被定义或者变量名是否书写错误
# print(a)
# ZeroDivisionError: division by zero (零不能做分母)
# a = 10
# print(a / 0)
# IndentationError: unexpected indent (缩进错误)
# 修改缩进,或者去调整函数关系
# a = 5
# b = 10
# SyntaxError: unexpected EOF while parsing (语法错误)
# 找到报错位置,查看语法是否存在问题,最好的办法就是将其进行格式化
# print(123
# TypeError: can only concatenate str (not "int") to str (数据类型错误)
# a = '123'
# print(a + 12)
# TypeError: can't multiply sequence by non-int of type 'str'
# 不同类型间的数据无法运算
# 在此情况下,我们需要进行数据类型转换
# 如(input接收的数据默认为字符串类型),需要转化为float
# ValueError: invalid literal for int() with base 10: '14.3'
# 字符串中是float类型数据,不可以转换为int类型
# print(int(str2))
# ValueError: invalid literal for int() with base 10: 'python'
# 字符串中是字符型数据,不可以转换为int类型
# NameError: name 'b' is not defined
# b必须已经被定义 b = b - 1 先计算b - 1 此时b必须存在
# TypeError: 'age' is an invalid keyword argument for print()
# =是赋值运算不能判断是否相等
# print(age = 12)
# print(str1.index('o', 5, 12)) # 10
# ValueError: substring not found
# 结论:找不到对应的子字符串,则会报错,如果能够查找到数据返回当前子字符串的正数索引
#优化将index函数变为find
# 进行join合并时,要注意可迭代类型中全部元素都要是字符串类型,否则无法合并
# print('❤'.join(list2))
# TypeError: sequence item 0: expected str instance, int found
这篇博客列举了Python编程中的一些常见错误,包括NameError、ZeroDivisionError、IndentationError、SyntaxError、TypeError、ValueError,并给出了相应的错误原因和解决建议。例如,NameError通常是变量未定义,ZeroDivisionError是除以零,IndentationError是缩进问题,而TypeError涉及到数据类型的不匹配。博主还提醒在处理字符串和数据类型转换时要特别注意,以避免出现错误。
1103

被折叠的 条评论
为什么被折叠?



