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 ...
随机推荐
- java中的四种权限
1.私有权限(private) private可以修饰数据成员,构造方法,方法成员,不能修饰类(此处指外部类,不考虑内部类).被private修饰的成员,只能在定义它们的类中使用,在其他类中不能调用. ...
- UIApplication的理解
iPhone应用程序是由主函数main启动,它负责调用UIApplicationMain函数,该函数的形式如下所示: int UIApplicationMain ( int argc, char *a ...
- JavaScript中label与break配合使用
语法 label: statement 说明 label语句可以在代码中添加标签,以便将来使用.定义的标签可以在将来由break或continue语句引用.加标签的语句一般都要与for语句等循环语句配 ...
- 小白学linux命令
小白是景女神全栈开发股份有限公司的一名财务实习员工,经过3个月的实习期,小白是过五关斩六将啊!终于成为了公司的一名正式员工,而且收到了景总亲自发来贺喜的邮件:“欢迎你加入大家庭,公司也本着员工全面发展 ...
- studio显示Surface: getSlotFromBufferLocked: unknown buffer: 0xa2a58be0
根据查询外网资料来看,出现这个错误的原因大致是换个模拟器或者物理机就可以了. 因为我使用的是安卓6.0,貌似都会出现这类的问题. 但是不影响程序运行.
- PyNest——Part1:neurons and simple neural networks
neurons and simple neural networks pynest – nest模拟器的界面 神经模拟工具(NEST:www.nest-initiative.org)专为仿真点神经元的 ...
- iOS 结构简单清晰的 设置页面
这个是也是看了人家的代码,觉得甚是简单清晰,也是比较容易扩展.拿来学习一下 效果展示: 重点有以下2处: 1 .建立groupModel 列清组元素:当前组list 集合, 是否有header 或者 ...
- 'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.
let newStr = String(str[..<index]) // = str.substring(to: index) In Swift 3 let newStr = String(s ...
- Spring session共享(使用redis)
SpringBoot+Redis实现HttpSession共享 前提:需要使用redis做session存储 一.效果演练(这里使用SpringBoot工程,Spring同理) 1.一个工程使用两个端 ...
- sql两张表关联更新字段
记录,推荐使用第三种,个人觉得比较好 --第一种写法,之前一直用,没有在意 UPDATE sr_t_TCodeUserReciveCfg SET fUserID=a.fUserID,fImportFl ...