- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章新手常见6种的python报错及解决方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
此篇文章整理新手编写代码常见的一些错误,有些错误是粗心的错误,但对于新手而已,会折腾很长时间才搞定,所以在此总结下我遇到的一些问题。希望帮助到刚入门的朋友们.
1.NameError变量名错误 。
报错:
1
2
3
4
|
>>>
print
a
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
NameError: name
'a'
is
not
defined
|
解决方案:
先要给a赋值。才能使用它。在实际编写代码过程中,报NameError错误时,查看该变量是否赋值,或者是否有大小写不一致错误,或者说不小心将变量名写错了.
注:在Python中,无需显示变量声明语句,变量在第一次被赋值时自动声明.
1
2
3
|
>>> a
=
1
>>>
print
a
1
|
2.IndentationError代码缩进错误 。
点击返回目录 。
代码:
1
2
3
4
|
a
=
1
b
=
2
if
a<b:
print
a
|
报错:
1
|
IndentationError: expected an indented block
|
原因:
缩进有误,python的缩进非常严格,行首多个空格,少个空格都会报错。这是新手常犯的一个错误,由于不熟悉python编码规则。像def,class,if,for,while等代码块都需要缩进.
缩进为四个空格宽度,需要说明一点,不同的文本编辑器中制表符(tab键)代表的空格宽度不一,如果代码需要跨平台或跨编辑器读写,建议不要使用制表符.
解决方案:
1
2
3
4
|
a
=
1
b
=
2
if
a<b:
print
a
|
3.AttributeError对象属性错误 。
报错:
1
2
3
4
5
6
|
>>>
import
sys
>>> sys.Path
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
AttributeError:
'module'
object
has no attribute
'Path'
|
原因:
sys模块没有Path属性.
解决方案:
python对大小写敏感,Path和path代表不同的变量。将Path改为path即可.
1
2
|
>>> sys.path
['
', '
/
usr
/
lib
/
python2.
6
/
site
-
packages']
|
python知识拓展:
使用dir函数查看某个模块的属性 。
。
。
4.TypeError类型错误 。
4.1入参类型错误 。
代码:
1
2
3
|
t
=
(
'a'
,
'b'
,
'c'
)
for
i
in
range
(t):
print
a[i]
|
报错:
TypeError: range() integer end argument expected, got tuple. 。
原因:
range()函数期望的入参是整型(integer),但却给的入参为元组(tuple) 。
解决方案:
将入参元组t改为元组个数整型len(t) 。
将range(t)改为range(len(t)) 。
4.2入参个数错误 。
4.2.1关于元组作为入参 。
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# coding=utf-8
'''
Created on 2016-7-21
@author: Jennifer
Project:显式等待
'''
from
selenium
import
webdriver
from
selenium.webdriver.common.by
import
By
from
selenium.webdriver.support.ui
import
WebDriverWait
from
selenium.webdriver.support
import
expected_conditions as EC
from
time
import
ctime
driver
=
webdriver.Firefox()
driver.get(r
'http://www.baidu.com/'
)
loc
=
(By.
ID
,
'kw'
)
print
ctime()
element
=
WebDriverWait(driver,
5
,
0.5
).until(EC.visibility_of_element_located(
*
loc))
element.send_keys(
'selenium'
)
print
ctime()
driver.quit()
|
报错:
1
2
3
4
5
|
Traceback (most recent call last):
File
"D:\system files\workspace\selenium\autotestcombat\test_4_7_1_webdriverwait.py"
, line
18
,
in
<module>
element
=
WebDriverWait(driver,
5
,
0.5
).until(EC.visibility_of_element_located(
*
loc))
TypeError: __init__() takes exactly
2
arguments (
3
given)
|
原因:
类的函数__init__()需要两个参数,但实际上给了三个。 EC.visibility_of_element_located类的入参应该是两个入参: self和元组。但却给了三个参数 self和*loc中的两个元素作为入参.
解决方案:
这里要将EC.visibility_of_element_located(*loc)改为EC.visibility_of_element_located(loc),入参为元组,而不是元组里边的两个值.
python知识拓展:
关于入参*的用法 以元组作为函数入参,如果元组前加*号,说明传递的入参为元组中的各个元素。如果元组前没有加*号,说明传递的入参为元组本身.
举例说明: loc =(By.NAME,'email') element1=WebDriverWait(driver,5,0.5).until(EC.visibility_of_element_located(loc)) #只要一个参数(不考虑self情况下),元组loc,即:(By.NAME,'email')。 直接传loc。 element2=driver.find_element(*loc)#需要两个参数,元组loc的元素,即:By.NAME,'email'。直接传*loc 。
4.2.2其他 。
报错:
1
2
3
4
5
6
|
>>>
import
os
>>> os.listdir()
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
TypeError: listdir() takes exactly
1
argument (
0
given)
|
原因:
listdir()函数需要一个入参,但是只给了0个入参.
解决方案:
加一个入参 。
1
2
|
>>> os.listdir(
'/home/autotest'
)
[
'hello.py'
,
'email126pro'
]
|
python知识拓展:
如何查看某个函数的使用,可以使用help查看.
1
2
3
4
5
6
7
8
9
|
>>>
help
(os.listdir)
Help
on built
-
in
function listdir
in
module posix:
listdir(...)
listdir(path)
-
> list_of_strings
Return a
list
containing the names of the entries
in
the directory.
path: path of directory to
list
|
说明:os.listdir()函数需要一个path路径入参,函数结果返回值是由字符串组成的列表.
4.3非函数却以函数来调用 。
报错:
1
2
3
4
5
|
>>> t
=
(
'a'
,
'b'
,
'c'
)
>>> t()
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
TypeError:
'tuple'
object
is
not
callable
|
原因:
t为元组,元组不能被调用,不能加()。初学者编写代码时,偶尔粗心会将变量当做方法来调用(不小心加了括号)。所以要认真检查下是否变量加了括号,或者方法漏加了括号.
解决方案:
将括号去除.
1
2
|
>>> t
(
'a'
,
'b'
,
'c'
)
|
5.IOError输入输出错误 。
5.1文件不存在报错 。
报错:
1
2
3
4
|
>>> f
=
open
(
"Hello.py"
)
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
IOError: [Errno
2
] No such
file
or
directory:
'Hello.py'
|
原因:
open()函数没有指明mode,默认为只读方式,如果该目录下没有Hello.py的文件,则会报错,可查看是否拼写有错误,或者是否大小写错误,或者根本不存在这个文件.
解决方案:
该目录下有hello.py文件,打开该文件即可.
1
2
3
4
5
6
7
8
9
|
>>> f
=
open
(
"hello.py"
)
python知识拓展:
如何查看python解释器当前路径:
>>>
import
os
>>> os.getcwd()
'/home/autotest'
|
查看python解释器当前路径下有哪些文件:
1
2
|
>>> os.listdir(
'/home/autotest'
)
[
'hello.py'
,
'email126pro'
]
|
5.2因文件权限问题报错 。
报错:
1
2
3
4
5
6
|
>>> f
=
open
(
"hello.py"
)
>>> f.write(
"test"
)
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
IOError:
File
not
open
for
writing
|
原因:
open("hello.py")如果入参没有加读写模式参数mode,说明默认打开文件的方式为只读方式,而此时又要写入字符,所以权限受限,才会报错.
解决方案:
更改模式 。
1
2
|
>>> f
=
open
(
"hello.py"
,
'w+'
)
>>> f.write(
"test"
)
|
6.KeyError字典键值错误 。
报错:
常见报错有,测试一接口,接口返回数据一般是json格式,而测试该接口校验某个值是否正确,如果key拼写错了,就会报KeyError。简单举例如下:
1
2
3
4
5
6
7
|
>>> d
=
{
'a'
:
1
,
'b'
:
2
,
'c'
:
3
}
>>>
print
d[
'a'
]
1
>>>
print
d[
'f'
]
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
KeyError:
'f'
|
解决方案:
访问d中有的键值,如a,b或c.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://www.cnblogs.com/yufeihlf/p/6068589.html#test0 。
最后此篇关于新手常见6种的python报错及解决方法的文章就讲到这里了,如果你想了解更多关于新手常见6种的python报错及解决方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!