Python运行的17个时新手常见错误小结
if , elif , else , for , while , class ,def 声明末尾添加 :(导致
“SyntaxError :invalid syntax”)
== 42
而不是 ==(导致“SyntaxError: invalid syntax”)
是赋值操作符而 == 是等于比较操作。该错误发生在如下代码中:
= 42:
indent”、“IndentationError:unindent does not match any outer
indetation level”以及“IndentationError:expected an indented
block”)
== 42:
== 42:
循环语句中忘记调用 len() (导致“TypeError: 'list' object cannot be interpreted
as an integer”)
range() 函数。要记得返回len 值而不是返回这个列表。
['cat', 'dog', 'mouse']
in range(spam):
'str' object does not support item assignment”)
'I have a pet cat.'
'r'
'I have a pet cat.'
spam[:13] + 'r' + spam[14:]
“TypeError: Can't convert 'int' object to str
implicitly”)
= 12
numEggs + ' eggs.')
= 12
str(numEggs) + ' eggs.')
= 12
eggs.' % (numEggs))
EOL while scanning string literal”)
= 'Al'
+ myName + . How are you?')
name 'fooba' is not defined”)
= 'Al'
+ fooba)
ruond(4.2)
Round(4.2)
“AttributeError: 'str' object has no attribute
'lowerr'”)
'THIS IS IN LOWERCASE.'
spam.lowerr()
list index out of range”)
['cat', 'dog', 'mouse']
{'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'}
my pet zebra is ' + spam['zebra'])
syntax”)
'algebra'
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
name 'foobar' is not defined”)
+= 1等于spam = spam + 1,这意味着spam需要指定一个有效的初始值。
0
42
42
local variable 'foobar' referenced before assignment”)
= 42
myFunction():
= 100
range()创建整数列表(导致“TypeError: 'range' object does not support item
assignment”)
range() 看上去是生成此列表的不错方式。然而,你需要记住 range() 返回的是 “range object”,而不是实际的
list 值。
range(10)
= -1
list(range(10))
= -1
Python 2 中 spam = range(10) 是能行的,因为在 Python 2 中 range()
返回的是list值,但是在 Python 3 中就会产生以上错误)
++ 或者 -- 自增自减操作符。(导致“SyntaxError: invalid syntax”)
, PHP 等其他的语言,也许你会想要尝试使用 ++ 或者 --
自增自减一个变量。在Python中是没有这样的操作符的。
1
1
1
myMethod() takes no arguments (1 given)”)
Foo():
myMethod():
Foo()
Python运行的17个时新手常见错误小结的更多相关文章
- <转>Python运行的17个时新手常见错误小结
1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”) 该错误将发生在 ...
- Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案
#Python运行Google App Engineer时出现的UnicodeDecodeError错误解决方案 ##问题描述 使用Python2.7.x运行GAE时有时会报这个错误 ```py ...
- 在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: ‘文件路径’
如题,在使用python语言的open函数时,提示错误OSError: [Errno 22] Invalid argument: '文件路径',在查阅了大量资料后也得到了一些解决方案,但是这些解决方案 ...
- PHP 中使用ajax时一些常见错误总结整理
这篇文章主要介绍了PHP 中使用ajax时一些常见错误总结整理的相关资料,需要的朋友可以参考下 PHP作为后端时,前端js使用ajax技术进行相互信息传送时,经常会出错误,对于新手来说有些手足无措.总 ...
- Python运行时的常见错误
1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加冒号(:)(导致 “SyntaxError :invalid syntax”) 2)使用 ...
- 【python】写csv文件时遇到的错误
1.错误 在许多文件中,写入csv文件时都加"wb",w指写入,b指二进制 如: csvwrite=csv.writer(open("output.csv",& ...
- [转]在SSIS中,使用“包配置”时的常见错误与解析
本文转自:http://www.cnblogs.com/invinboy/archive/2008/05/26/1034312.html 在以前的DTS中,在包的开发.测试.发布迁移过程中你必须手动的 ...
- npm install依赖时,常见错误
1.npm install依赖时,报错:npm ERR! Unexpected end of JSON input while parsing near '...gin":"^1. ...
- Android Studio 新手常见错误:Gradle DSL method not found: 'runProguard()'
在Android Studio上执行Github上的某Android开源项目,提示报错: Error:(20, 0) Gradle DSL method not found: 'runProguard ...
随机推荐
- Axis2Service客户端访问通用类集合List自定义类型
Axis2 服务四种客户端调用方式: 1.AXIOMClient 2.generating a client using ADB 3.generating a client using XMLBean ...
- JavaScript数据结构与算法-队列练习
队列的实现 // 队列类 function Deque () { this.dataStore = []; this.enqueueFront = enqueueFront; this.enqueue ...
- python面向对象(一)
什么是面向对象的程序设计及为什么要有它 面向过程的程序设计:核心是过程二字,过程指的是解决问题的步骤,即先干什么再干什么......面向过程的设计就好比精心设计好一条流水线,是一种机械式的思维方式. ...
- 20170401 错了两天的-XML解析
你不找到的话,错误就在那里.你找到了错误才会成为财富! Strans XML 解析3要素:1.源xml 格式正常, eg. '<?xml version="1.0" enco ...
- Ip-san 配置过程
1:SAN的定义 SAN是storage area network(存储区域网络)的简写,早期的san采用的是光纤通道技术,后期当iscsi协议出现以后,为了区分两者,就划分了IP SAN和FC SA ...
- iOS 几种常用的 crash log 崩溃信息调试方法
前言:crash log 对 定位崩溃问题 ,并且不容易复现,尤其是及时对appstore 上正在运营的 app 的迭代改进来说 非常重要. 1 crash两种情况 1.1 测试环境下 追踪bug 1 ...
- $Android去除系统默认的标题栏和全屏的三种方法
在做应用的时候,很多时候是不需要系统自带的标题栏的,而是自己去实现标题栏,这就要去掉系统的标题栏,下面总结了三种方法.全屏也是一样的道理,也总结了实现的三种方法. (一)去除标题栏 1.方法1 在Ac ...
- Python编程-模块和包
一.模块 1.什么是模块? 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 2.为何要使用模块? 如果你退出python解释器然后重新进入,那么你之前定义的函 ...
- library-type:fr-unstanded vs fisrt-stand vs second-stanrd
建库时是否是链特异性建库. Tophat2: --library-type The default is unstranded (fr-unstranded). If either fr-firsts ...
- mysql的一些密码错误问题,并从windows登录linux主机的mysql
mysqld_safe --skip-grant-tables& mysql -u root mysql 可以修改密码: mysql>update mysql.user set auth ...