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 ...
随机推荐
- VS2010程序崩溃- APPCRASH
使用VS2010打开某个项目出现错误,程序崩溃:还不是全部的项目:开始以为是那个项目本身有什么问题,查了很久发现不是这样的 程序崩溃提示 问题签名: 问题事件名称: APPCRASH 应用程序名: d ...
- 20160916-3:mysql主从复制
一.什么是主从复制 将一个数据库节点的数据拷贝到一个或多个数据库节点(主节点—>从节点) 二.主从复制的原理 [简述]:将主节点上的变更操作存储到binlog,从节点建立了到主节点的复制关系后, ...
- 数组中binarySearch 中小小的误区! 用的时候大家要注意点
- this.$apply()
chooseVideo(e) { this.fileInfo = {} let that = this wx.chooseVideo({ sourceType: ['album', 'camera'] ...
- jq cookie
//$.cookie("xx");//读取xx的值 //$.cookie("xx","123");//设置xx的值为123 //$.cook ...
- QCache 缓存(模板类,类似于map,逻辑意义上的缓存,方便管理,和CPU缓存无关。自动获得被插入对象的所有权,超过一定数量就会抛弃某些值)
在软件开发中,我们经常需要在内存中存储一些临时数据用于后续相关计算.我们一般把这些数据存储到某个数组里,或者STL中的某个合适的容器中.其实,在Qt中直接为我们提供了一个QCache类专用于这种需求. ...
- Python利用subprocess起进程
from multiprocessing import Process, Pool import time import subprocess def task(msg): print 'hello, ...
- Eclipse解决JSP页面乱码问题
解决方案 1.首先选择 Window --> Preferences --> General--> Content Types --> text (点击左边的+,显示 ...
- C#基础之匿名方法实例教程
本文以实例形式讲解了C#的匿名方法的用法,分享给大家供大家参考之用.具体如下: 匿名方法是C# 2.0的语言新特性.首先看个最简单的例子: class Program { static void Ma ...
- mysql之视图,触发器,事务等
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...