我是靠谱客的博主 大意睫毛,最近开发中收集的这篇文章主要介绍使用python中常见的错误及处理办法(1),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

(1)invalid literal for int( ) with base 10:'largeDoses'

解决办法:这是因为数据类型不一致,导致出错,在int/str/float转换,其中raw_input接收字符串,input接收整数

(2)‘                      ^

       syntaxError:invalid syntax‘

解决办法:可能是标点符号、括号、双引号等不配对,需要增加或者删除

(3)‘ NameError:name'sorted Dist Indicies' is not defined ’

解决办法:这是因为变量前后不匹配,需要增加或者减少某个字母;有可能是前后大小写不同而引起不能识别

(4)‘dict_keys’ object does't support indexing

解决办法:这是因为python版本不一致,所以对列表的索引出现问题

例:firstsides = mytree.keys( )[0] 应该改为:firstsides = list(mytree.keys( ))[0]

(5)‘write( ) argument must be str, not byes’

解决办法:pickle( ) 是二进制格式,故带'b'标志打开文件‘wb/rb’

(6)同一目录下相互调用函数的做法是

-------先import 文件名

-------再文件名.函数

例:import treeplotter

       treeplotter.createplot(lensesTree)

(7)‘date type not understood!’

例:returnMat  = zeros(numberoflines, 3)

修改为:returnMat  = zeros((numberoflines, 3)),别忘了外面的括号!!!

(8)‘ return arrays must be of ArrayType’

例:shannoEnt -= prob * log(prob,2)

修改为:shannoEnt -= prob * np.math.log(prob,2)

(9)’can't multiply sequence by non-int of type float‘’

解决办法:需要将出错的地方转换数据类型

(10)'itertools cycle' object has no attribute 'next'

原因:编辑版本的原因,出现差错

例:idexer.next( )

修改为:next(idexer)

(11)‘list index out of range’

原因:(1)list[index],index超出范围

           (2)list是空列表,进行list[0]就会出错

(12)‘‘int’ object is not iterable’

例:for i in hosts:

修改为:for i in range(hosts):

(13)'unindent does not mach any outer indentation level '

原因:注意缩进,在某行的开头处可能被空格了一下

(14)'could not convert string to float '

原因:(1)可能是文本格式的问题----数据应先在外边的文本.txt编辑后再导入,否则就会引起这种错误的提示

   (2)可能在数据分割时出错,将split('n')改成split(',')

(15)'only length-1 arrays can be converted to python scalars'

原因:不要将列表转换成矩阵形式,

例:math.log(a) ----->np.log(a)

(16)list( ) takes at most 1 argument (2 given)

原因:list 至多1个参数








最后

以上就是大意睫毛为你收集整理的使用python中常见的错误及处理办法(1)的全部内容,希望文章能够帮你解决使用python中常见的错误及处理办法(1)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部