资讯内容
看看Python 高手都写不出来的几个错误

对于刚入门的 pythonista 在学习过程中运行代码是或多或少会遇到一些错误,刚开始可能看起来比较费劲。随着代码量的积累,熟能生巧当遇到一些运行时错误时能够很快的定位问题原题。下面整理了一些常见的17个错误,等你写出的代码不怎么出现这些错误的时候,你的 Python 功力就上一个台阶了。换一句话说,当你成为一名合格的 Python 开发者后,你可能 “都写不出” 这类的错误。epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
免费学习推荐:python视频教程epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
1、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
忘记在if,for,def,elif,else,class等声明末尾加:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
会导致“SyntaxError :invalid syntax”如下:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
if spam == 42 print('Hello!')2、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
使用= 而不是 ==epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
也会导致“SyntaxError: invalid syntax”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
= 是赋值操作符而 == 是等于比较操作。该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
if spam = 42: print('Hello!')3、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
错误的使用缩进量epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
print('Hello!') print('Howdy!')或者:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
if spam == 42: print('Hello!') print('Howdy!')4、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
在 for 循环语句中忘记调用 len()epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“TypeError: 'list' object cannot be interpreted as an integer”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
通常你想要通过索引来迭代一个list或者string的元素,这需要调用 range() 函数。要记得返回len 值而不是返回这个列表。epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
spam = ['cat', 'dog', 'mouse'] for i in range(spam): print(spam[i])5、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
尝试修改string的值epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“TypeError: 'str' object does not support item assignment”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
string是一种不可变的数据类型,该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
spam = 'I have a pet cat.' spam[13] = 'r' print(spam)而正确做法是:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
spam = 'I have a pet cat.' spam = spam[:13] + 'r' + spam[14:] print(spam)6、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
尝试连接非字符串值与字符串epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致 “TypeError: Can't convert 'int' object to str implicitly”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
numEggs = 12 print('I have ' + numEggs + ' eggs.')而正确做法是:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
numEggs = 12 print('I have ' + str(numEggs) + ' eggs.') numEggs = 12 print('I have %s eggs.' % (numEggs))7、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
在字符串首尾忘记加引号epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“SyntaxError: EOL while scanning string literal”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
print(Hello!') print('Hello!) myName = 'Al' print('My name is ' + myName + . How are you?')8、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
变量或者函数名拼写错误epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“NameError: name 'fooba' is not defined”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
foobar = 'Al' print('My name is ' + fooba) spam = ruond(4.2) spam = Round(4.2)9、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
方法名拼写错误epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致 “AttributeError: 'str' object has no attribute 'lowerr'”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
spam = 'THIS IS IN LOWERCASE.' spam = spam.lowerr()10、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
引用超过list**大索引epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“IndexError: list index out of range”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
spam = ['cat', 'dog', 'mouse'] print(spam[6])11、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
使用不存在的字典键值epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“KeyError:‘spam’”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'} print('The name of my pet zebra is ' + spam['zebra'])12、 epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
尝试使用Python关键字作为变量名epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“SyntaxError:invalid syntax”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
Python关键不能用作变量名,该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
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, yield13、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
在一个定义新变量中使用增值操作符epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“NameError: name 'foobar' is not defined”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
不要在声明变量时使用0或者空字符串作为初始值,这样使用自增操作符的一句spam += 1等于spam = spam + 1,这意味着spam需要指定一个有效的初始值。epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
spam = 0 spam += 42 eggs += 4214、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
在定义局部变量前在函数中使用局部变量(此时有与局部变量同名的全局变量存在)epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“UnboundLocalError: local variable 'foobar' referenced before assignment”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
在函数中使用局部变来那个而同时又存在同名全局变量时是很复杂的,使用规则是:如果在函数中定义了任何东西,如果它只是在函数中使用那它就是局部的,反之就是全局变量。epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
这意味着你不能在定义它之前把它当全局变量在函数中使用。epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
someVar = 42 def myFunction(): print(someVar) someVar = 100 myFunction()15、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
尝试使用 range()创建整数列表epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“TypeError: 'range' object does not support item assignment”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
有时你想要得到一个有序的整数列表,所以 range() 看上去是生成此列表的不错方式。然而,你需要记住 range() 返回的是 “range object”,而不是实际的 list 值。epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
spam = range(10) spam[4] = -1 正确写法: spam = list(range(10)) spam[4] = -1(注意:在 Python 2 中 spam = range(10) 是能行的,因为在 Python 2 中 range() 返回的是list值,但是在 Python 3 中就会产生以上错误)epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
16、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
不存在 ++ 或者 -- 自增自减操作符。epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“SyntaxError: invalid syntax”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
如果你习惯于例如 C++ , Java , PHP 等其他的语言,也许你会想要尝试使用 ++ 或者 -- 自增自减一个变量。在Python中是没有这样的操作符的。epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
spam = 1spam++ 正确写法: spam = 1spam += 117、epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
忘记为方法的第一个参数添加self参数epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
导致“TypeError: myMethod() takes no arguments (1 given)”epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
该错误发生在如下代码中:epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
class Foo(): def myMethod(): print('Hello!') a = Foo() a.myMethod()相关免费学习推荐:python教程(视频)epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
以上就是看看Python 高手都写不出来的几个错误的详细内容,更多请关注少儿编程网其它相关文章!epe少儿编程网-Scratch_Python_教程_免费儿童编程学习平台
- 上一篇
python学习requests爬取网页图片
简介免费学习推荐:python视频教程前言最近想做一个练练的小游戏给家里的小朋友玩儿,但是苦于选取素材,然后在一个巨佬的博客里找了灵感,就借用一下粉丝的头像试试爬取网页里的图片吧!(感谢各位啦!)完成总目标:爬取粉丝头像作为素材,完成一个连连看的小游戏故本文分为两部分内容:1、爬取素材部分;2、利用素材
- 下一篇
Python地理数据处理之分析使用GR进行矢量
简介免费学习推荐:python视频教程1、叠加分析叠加分析操作:plot颜色:‘r’红色,‘g’绿色,‘b’蓝色,‘c’青色,‘y’黄色,‘m’品红,‘k’黑色,‘w’白色。新奥尔良城市边界、水体和湿地的简单地图:1.新奥尔良城市沼泽区域分析:importosfromosgeoimportogrfrom