在Python中 出现的错误类型以及解决办法

#IndentationError:expected an indented #block 缩进错误
#解决办法 tab 缩进
for index in range(10):
    if name == '小王':
        print('nothing')
    else:
    print('nothing') 
name ='小王'
age = 16
print('我的名字是'+name+'我的年龄是'+age)
#TypeError:must be str,not int
#类型错误
#解决办法 使用拼接的时候 必须是字符串 或#者数字转换成字符串
count = 0
while True:
    count+=1
    if count ==20:
        return
#SyntaxError:'return'outside function
#return 不能在方法以外使用
#解决办法:把 return放在方法以内使用
content = 'hello world'
result = content.index('r')
print(result)
#ValueError:substring not found
#值错误  子字符串未找到
content ='hello world'
print(content[21])
#IndexError:string index out of range
#索引错误  字符串的长度超出了范围
#解决办法 查看字符串的长度  索引要小于长度

list1 = ['autman','小李子','诺兰','皮克斯']
print(list[5])
#TypeError:'list'object is not callable
#索引错误 列表索引超出范围

tp1 = ((),[],{},1,a,3.14,True)
tp1.remove(1)
#AttributeError:'tuple'object has no attribure 'remove'
#属性错误  元组对象没有属性'remove'
dic1 = {
    'name':'张三',
    'age':17,
    'friend':['李四','王五','赵六']
}
#KeyError:'fond'
#key 键错误 没有指定的键值'fond'
print(dic1['fond'])

dic1.pop()
#TypeError:pop expected at least 1 argument,got 0
#argument 参数 expected 期望 at least 至少
#类型错误:pop方法希望得到至少一个参数  但是现在参数为0
date = date1.strftime('%y 年%m月 %d日 %H时 %M分 %S秒')
# UnicodeEncodeError: 'locale' codec #can't encode character '\u5e74' in #position 3: Illegal byte sequence
# 编码错误:本地文件不能对指定位置的字符进行编码
#解决方法:用replace进行代替
date2 = date2.replace('year','年').replace('month','月').replace('day','日')
print(date2)







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值