小白如何搭建个人网站,wordpress 获取菜单,游戏源码平台,手机wap网站模板使用python学习过程遇到一些问题记录#xff1a; 1、 IndentationError:expected an indented block错误的解决办法 一句话 有冒号的下一行往往要缩进#xff0c;该缩进就缩进 参考资料#xff1a;http://blog.csdn.net/hongkangwl/article/details/16344749 2、17个新手常见Pyt… python学习过程遇到一些问题记录 1、 IndentationError:expected an indented block错误的解决办法 一句话 有冒号的下一行往往要缩进该缩进就缩进 参考资料http://blog.csdn.net/hongkangwl/article/details/16344749 2、17个新手常见Python运行时错误 初学 Python 时想要弄懂 Python 的错误信息的含义可能有点复杂。这里列出了常见的的一些让你程序 crash 的运行时错误。 1忘记在 if , elif , else , for , while , class ,def 声明末尾添加 导致 “SyntaxError invalid syntax” 该错误将发生在类似如下代码中 if spam 42print(Hello!) 2)使用 而不是 导致“SyntaxError: invalid syntax” 是赋值操作符而 是等于比较操作。该错误发生在如下代码中 if spam 42:print(Hello!) 3错误的使用缩进量。导致“IndentationErrorunexpected indent”、“IndentationErrorunindent does not match any outer indetation level”以及“IndentationErrorexpected an indented block” 记住缩进增加只用在以结束的语句之后而之后必须恢复到之前的缩进格式。该错误发生在如下代码中 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 值而不是返回这个列表。 该错误发生在如下代码中 spam [cat, dog, mouse]for i in range(spam):print(spam[i]) 5尝试修改string的值导致“TypeError: str object does not support item assignment” string是一种不可变的数据类型该错误发生在如下代码中 spam I have a pet cat.spam[13] rprint(spam) 而你实际想要这样做 spam I have a pet cat.spam spam[:13] r spam[14:]
print(spam) 6尝试连接非字符串值与字符串导致 “TypeError: Cant convert int object to str implicitly” 该错误发生在如下代码中 numEggs 12print(I have numEggs eggs.) 而你实际想要这样做 numEggs 12print(I have str(numEggs) eggs.)或者numEggs 12print(I have %s eggs. % (numEggs)) 7在字符串首尾忘记加引号导致“SyntaxError: EOL while scanning string literal” 该错误发生在如下代码中 print(Hello!)或者:print(Hello!)或者:myName Alprint(My name is myName . How are you?) 8变量或者函数名拼写错误导致“NameError: name fooba is not defined” 该错误发生在如下代码中 foobar Alprint(My name is fooba)或者:spam ruond(4.2)或者:spam Round(4.2) 9方法名拼写错误导致 “AttributeError: str object has no attribute lowerr” 该错误发生在如下代码中 spam THIS IS IN LOWERCASE.spam spam.lowerr() 10引用超过list最大索引导致“IndexError: list index out of range” 该错误发生在如下代码中 spam [cat, dog, mouse]
print(spam[6]) 11使用不存在的字典键值导致“KeyError‘spam’” 该错误发生在如下代码中 spam {cat: Zophie, dog: Basil, mouse: Whiskers}
print(The name of my pet zebra is spam[zebra]) 12尝试使用Python关键字作为变量名导致“SyntaxErrorinvalid syntax” 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, yield 13在一个定义新变量中使用增值操作符导致“NameError: name foobar is not defined” 不要在声明变量时使用0或者空字符串作为初始值这样使用自增操作符的一句spam 1等于spam spam 1这意味着spam需要指定一个有效的初始值。 该错误发生在如下代码中 spam 0spam 42eggs 42 14在定义局部变量前在函数中使用局部变量此时有与局部变量同名的全局变量存在导致“UnboundLocalError: local variable foobar referenced before assignment” 在函数中使用局部变来那个而同时又存在同名全局变量时是很复杂的使用规则是如果在函数中定义了任何东西如果它只是在函数中使用那它就是局部的反之就是全局变量。 这意味着你不能在定义它之前把它当全局变量在函数中使用。 该错误发生在如下代码中 someVar 42def myFunction():print(someVar)someVar 100myFunction() 15尝试使用 range()创建整数列表导致“TypeError: range object does not support item assignment” 有时你想要得到一个有序的整数列表所以 range() 看上去是生成此列表的不错方式。然而你需要记住 range() 返回的是 “range object”而不是实际的 list 值。 该错误发生在如下代码中 spam range(10)
spam[4] -1 也许这才是你想做 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中是没有这样的操作符的。 该错误发生在如下代码中 spam 1spam 也许这才是你想做的 spam 1spam 1 17忘记为方法的第一个参数添加self参数导致“TypeError: myMethod() takes no arguments (1 given)” 该错误发生在如下代码中 class Foo():def myMethod():print(Hello!)
a Foo()
a.myMethod() 3、python 导入模块出错 ImportError: No module named request import request 解决方法pip install requests 4、Python3中已经安装了bs4Beautifulsoup 4了但是却还是出错ImportError: No module named BeautifulSoup BeautifulSoup 的安装 安装方法 1 apt-get install python-bs4 2 easy_install beautifulsoup4 3 pip install beautifulsoup4 4 源码安装 python setup.py install 根据不同的操作系统选用不同的安装方法这些方法都能安装成功不同点在于安装的工具不同。 采用的是第四种安装方法下面我来简要介绍下第四种安装方法 curl http://www.crummy.com/software/BeautifulSoup/bs4/download/4.1/beautifulsoup4-4.1.2.tar.gz beautifulsoup4-4.1.2.tar.gz tar zxvf beautifulsoup4-4.1.2.tar.gz cd beautifulsoup4-4.1.2 python setup.py install Ok 你就能看到安装信息提示安装成功。 安装成功肯定想迫不及待的使用你打开 python command 窗口你很 happy 的输入 参考资料 http://isilic.iteye.com/blog/1741918 http://www.crifan.com/python3_after_install_bs4_still_error_importerror_no_module_named_beautifulsoup/ Beautiful源码下载地址 https://www.crummy.com/software/BeautifulSoup/bs4/download/ python多版本共存问题 http://www.cnblogs.com/Yiutto/p/5962906.html 本文转自 boy461205160 51CTO博客原文链接:http://blog.51cto.com/461205160/1933525