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 ...
随机推荐
- 《从零开始学Swift》学习笔记(Day48)——类型检查与转换
原创文章,欢迎转载.转载请注明:关东升的博客 继承会发生在子类和父类之间,是一系列类的继承关系. 例如:Person是类层次结构中的根类,Student是Person的直接子类,Worker是Pers ...
- 【转】Linux系统上安装MySQL 5.5 rpm
1.准备工作 从MySQL官网上分别下载mysql服务器端于客户端包. 如: MySQL-server-5.5.15-1.linux2.6.x86_64.rpm和MySQL-client-5.5.15 ...
- a标签里面包含img标签,出现a标签的高度高于img标签好几个px
问题:a标签里面包含img标签,出现a标签的高度高于img标签好几个px 解决: a元素下有一个匿名文本,这个文本外有一个匿名行级盒子,它有的默认vertical-align是baseline的,而且 ...
- 请听一个故事------>你真的认为iPhone只是一部手机?苹果惊天秘密!!
在网上看到的一篇小说,感觉有点意思,转载过来大家一起围观下,作者很幽默很风趣. 导读:iPhone的隐藏功能!Jobs的军方身份!图灵服毒自杀的传奇故事!中兴华为的神秘背景! 你真的认为iPhone只 ...
- 我的第一个JSP
首先在新建一个Webproject 再在WebRoot以下new一个HelloWorld.jsp 改动body里面的内容 <body> <h1>HelloWorld:& ...
- CSS让一个元素一闪一闪的
.heart{ animation:heart 1s ease infinite; } @keyframes heart { 0% {opacity:0.1;} 100%{;} }
- shell方式切割tomcat日志
#!/bin/bash while true do cd /usr/local/tomcat/logs d=`date +%Y%m%d` d15=`date -d'15 day ago' +%Y%m% ...
- LeetCode-day01&02
感觉还好,坚持住就行,毕竟智商不够 1. Length of Last Word求一个数组的最后一个单词的长度 2. Plus One 大数加1 3. Add Binary 二进制加法 4. S ...
- django中的django admin插件
一.django admin前言 1.admin的作用 为了方便后台开发者快速的部署测试环境,于是就产生了admin,admin主要是操作models中的类从而实现对数据库中的数据增删改查的操作. 2 ...
- List contents of directories in a tree-like format
Python programming practice. Usage: List contents of directories in a tree-like format. #!/usr/bin/p ...