Python错误:TypeError和ValueError实例
TypeError
是Python中常见的类型错误,当一个操作或者函数应用于不兼容的数据类型时,就会产生这种错误。
例如:
# 错误示例1
num = "10" # 字符串
print(num + 5) # TypeError: can only concatenate str (not "int") to str
# 错误示例2
list_obj = [1, 2, 3] # 列表
print(list_obj * 2)) # ValueError: cannot multiply sequence by non-int of type 'list'
ValueError
也是Python中常见的错误类型,它通常发生在预期的值或数据类型不正确的时候。
例如:
# 错误示例3
num = "10" # 字符串
print(num + 5.6)) # TypeError: can only concatenate str (not "float") to str
以上就是TypeError
和ValueError
在Python中的实例。
还没有评论,来说两句吧...