【转载】17个新手常见Python运行时错误
转自:http://www.cnblogs.com/smile-ls/archive/2013/05/20/3088393.html
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂。这里列出了常见的的一些让你程序 crash 的运行时错误。
1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”)
该错误将发生在类似如下代码中:
1
2
|
if spam = = 42 print ( 'Hello!' ) |
2)使用 = 而不是 ==(导致“SyntaxError: invalid syntax”)
= 是赋值操作符而 == 是等于比较操作。该错误发生在如下代码中:
1
2
|
if spam = 42 : print ( 'Hello!' ) |
3)错误的使用缩进量。(导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”)
记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
print ( 'Hello!' ) print ( 'Howdy!' ) 或者: if spam = = 42 : print ( 'Hello!' ) print ( 'Howdy!' ) 或者: if spam = = 42 : print ( 'Hello!' ) |
4)在 for 循环语句中忘记调用 len() (导致“TypeError: 'list' object cannot be interpreted as an integer”)
通常你想要通过索引来迭代一个list或者string的元素,这需要调用 range() 函数。要记得返回len 值而不是返回这个列表。
该错误发生在如下代码中:
1
2
3
|
spam = [ 'cat' , 'dog' , 'mouse' ] for i in range (spam): print (spam[i]) |
5)尝试修改string的值(导致“TypeError: 'str' object does not support item assignment”)
string是一种不可变的数据类型,该错误发生在如下代码中:
1
2
3
|
spam = 'I have a pet cat.' spam[ 13 ] = 'r' print (spam) |
而你实际想要这样做:
1
2
3
|
spam = 'I have a pet cat.' spam = spam[: 13 ] + 'r' + spam[ 14 :] print (spam) |
6)尝试连接非字符串值与字符串(导致 “TypeError: Can't convert 'int' object to str implicitly”)
该错误发生在如下代码中:
1
2
|
numEggs = 12 print ( 'I have ' + numEggs + ' eggs.' ) |
而你实际想要这样做:
1
2
3
4
5
6
7
|
numEggs = 12 print ( 'I have ' + str (numEggs) + ' eggs.' ) 或者: numEggs = 12 print ( 'I have %s eggs.' % (numEggs)) |
7)在字符串首尾忘记加引号(导致“SyntaxError: EOL while scanning string literal”)
该错误发生在如下代码中:
1
2
3
4
5
6
7
8
9
10
|
print (Hello!') 或者: print ('Hello!) 或者: myName = 'Al' print ( 'My name is ' + myName + . How are you?') |
8)变量或者函数名拼写错误(导致“NameError: name 'fooba' is not defined”)
该错误发生在如下代码中:
1
2
3
4
5
6
7
8
9
10
|
foobar = 'Al' print ( 'My name is ' + fooba) 或者: spam = ruond( 4.2 ) 或者: spam = Round ( 4.2 ) |
9)方法名拼写错误(导致 “AttributeError: 'str' object has no attribute 'lowerr'”)
该错误发生在如下代码中:
1
2
|
spam = 'THIS IS IN LOWERCASE.' spam = spam.lowerr() |
10)引用超过list最大索引(导致“IndexError: list index out of range”)
该错误发生在如下代码中:
1
2
|
spam = [ 'cat' , 'dog' , 'mouse' ] print (spam[ 6 ]) |
11)使用不存在的字典键值(导致“KeyError:‘spam’”)
该错误发生在如下代码中:
1
2
|
spam = { 'cat' : 'Zophie' , 'dog' : 'Basil' , 'mouse' : 'Whiskers' } print ( 'The name of my pet zebra is ' + spam[ 'zebra' ]) |
12)尝试使用Python关键字作为变量名(导致“SyntaxError:invalid syntax”)
Python关键不能用作变量名,该错误发生在如下代码中:
1
|
class = 'algebra' |
Python3的关键字有:and, as, assert, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield
13)在一个定义新变量中使用增值操作符(导致“NameError: name 'foobar' is not defined”)
不要在声明变量时使用0或者空字符串作为初始值,这样使用自增操作符的一句spam += 1等于spam = spam + 1,这意味着spam需要指定一个有效的初始值。
该错误发生在如下代码中:
1
2
3
|
spam = 0 spam + = 42 eggs + = 42 |
14)在定义局部变量前在函数中使用局部变量(此时有与局部变量同名的全局变量存在)(导致“UnboundLocalError: local variable 'foobar' referenced before assignment”)
在函数中使用局部变来那个而同时又存在同名全局变量时是很复杂的,使用规则是:如果在函数中定义了任何东西,如果它只是在函数中使用那它就是局部的,反之就是全局变量。
这意味着你不能在定义它之前把它当全局变量在函数中使用。
该错误发生在如下代码中:
1
2
3
4
5
|
someVar = 42 def myFunction(): print (someVar) someVar = 100 myFunction() |
15)尝试使用 range()创建整数列表(导致“TypeError: 'range' object does not support item assignment”)
有时你想要得到一个有序的整数列表,所以 range() 看上去是生成此列表的不错方式。然而,你需要记住 range() 返回的是 “range object”,而不是实际的 list 值。
该错误发生在如下代码中:
1
2
|
spam = range ( 10 ) spam[ 4 ] = - 1 |
也许这才是你想做:
1
2
|
spam = list ( range ( 10 )) spam[ 4 ] = - 1 |
(注意:在 Python 2 中 spam = range(10) 是能行的,因为在 Python 2 中 range() 返回的是list值,但是在 Python 3 中就会产生以上错误)
16)不错在 ++ 或者 -- 自增自减操作符。(导致“SyntaxError: invalid syntax”)
如果你习惯于例如 C++ , Java , PHP 等其他的语言,也许你会想要尝试使用 ++ 或者 -- 自增自减一个变量。在Python中是没有这样的操作符的。
该错误发生在如下代码中:
1
2
|
spam = 1 spam + + |
也许这才是你想做的:
1
2
|
spam = 1 spam + = 1 |
17)忘记为方法的第一个参数添加self参数(导致“TypeError: myMethod() takes no arguments (1 given)”)
该错误发生在如下代码中:
1
2
3
4
5
|
class Foo(): def myMethod(): print ( 'Hello!' ) a = Foo() a.myMethod() |
【转载】17个新手常见Python运行时错误的更多相关文章
- [转]17个新手常见Python运行时错误
原址:http://www.oschina.net/question/89964_62779?p=1 当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的 ...
- 17个新手常见Python运行时错误
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...
- python的Error集,17个新手常见Python运行时错误
python及相关工具安装Error集 . 如果升级python版本中出现error .so.1.0: cannot open shared object file: No such file or ...
- python--17个新手常见Python运行时错误
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...
- 新手常见Python运行时错误
经过整理与在实际中遇到的问题,将新手经常遇到的汇总下,以便自己犯傻又这么干了 1)"SyntaxError :invalid syntax",语法错误 A.查看是否在 if , e ...
- 常见Python运行时错误
1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”) 该错误将发生在 ...
- 常见的Python运行时错误
date: 2020-04-01 14:25:00 updated: 2020-04-01 14:25:00 常见的Python运行时错误 摘自 菜鸟学Python 公众号 1. SyntaxErro ...
- 如何免安装使用 Python?推荐 17 个在线的 Python 解释器!
作者:Al Sweigart 译者:豌豆花下猫@Python猫 英文:https://inventwithpython.com/blog/2022/10/30/17-online-python-ide ...
- 常见Python爬虫工具总结
常见Python爬虫工具总结 前言 以前写爬虫都是用requests包,虽然很好用,不过还是要封装一些header啊什么的,也没有用过无头浏览器,今天偶然接触了一下. 原因是在处理一个错误的时候,用到 ...
随机推荐
- linux下(centos7)docker安装
参考链接https://docs.docker.com/install/linux/docker-ce/centos/ 第一步,确定是centos7及以上版本 cat /etc/redhat-rele ...
- 硬件知识整理part4--0欧电阻在电路中的应用
逝者如斯夫,不舍昼夜. --<论语子罕篇> 说起0欧电阻,必须先铺垫一下电路中的各种地. 先说一下,地是什么??地是参考0电位,所有电压都是参考地得出的,地的标准要一致,故各种地应短接在一 ...
- Vue整合d3.v5.js制作--折线图(line)
先上效果图(x轴固定为时间轴): 图中出现的悬浮框是鼠标悬停效果 1.环境说明: vue版本:"vue": "^2.5.2" d3版本:"d3&quo ...
- AGC011-E Increasing Numbers
题意 给定一个数\(n\),\(n≤10^{500,000}\),问\(n\)最少可以拆分成几个不降数的和.一个不降数是在十进制位下,从高位往低位看,每个数都不会比高位的数更小的数 做法 不降数可以拆 ...
- GCD相关
板子: ? gcd(b, a % b) : a; } POJ1930 题意:给你一个无限循环小数,给到小数点后 9 位,要求这个数的分数形式. 解法: 要想解决这道题,首先应该了解如何将循环小数化为分 ...
- JMeter接口测试-跨线程组取参数值的两种方法
前言 如果你工作中已经在用jmeter做接口测试,或性能测试了,你可能会遇到一个麻烦.那就是jmeter的变量值不能跨线程组传递.如果变量在开始测试之前已经确定了,我们可以用参数化关联就可以实现. 但 ...
- 面试再问ThreadLocal,别说你不会!
ThreadLocal是什么 以前面试的时候问到ThreadLocal总是一脸懵逼,只知道有这个哥们,不了解他是用来做什么的,更不清楚他的原理了.表面上看他是和多线程,线程同步有关的一个工具类,但其 ...
- HTML单词
html超文本标记语言 head 头部font 字体 字形i(italic) 倾斜,斜体字big 大的,字体加大hr 水平线Pre(predefined)预定义h5标题5Div(division)区隔 ...
- 一个抓猫的游戏 消遣GAME 持续更新中!
一个抓猫的游戏 版本 Catch_Cat_V0.30 https://files-cdn.cnblogs.com/files/send-off-a-friend/Catch_Cat_V0.3.rar ...
- (node:7584) UnhandledPromiseRejectionWarning: MongooseTimeoutError: Server selection timed out after 30000 ms
记录一次学习node.js犯的低级错误 这里遇到一个这样的问题 express连接mongoose时报错(node:7584) UnhandledPromiseRejectionWarning: Mo ...