2018-11-02
阅读量:
816
一些让你程序 crash 的运行错误:
1)尝试连接非字符串值与字符串(导致 “TypeError: Can't convert 'int' object to str implicitly”)
该错误发生在如下代码中:
numEggs = 12
print('I have ' + numEggs + ' eggs.')
而你实际想要这样做:
numEggs = 12
print('I have ' + str(numEggs) + ' eggs.')
或者:
numEggs = 12
print('I have %s eggs.' % (numEggs))
2)在字符串首尾忘记加引号(导致“SyntaxError: EOL while scanning string literal”)
该错误发生在如下代码中:
print(Hello!')
或者:
print('Hello!)
或者:
myName = 'Al'
print('My name is ' + myName + . How are you?')
3)变量或者函数名拼写错误(导致“NameError: name 'fooba' is not defined”)
该错误发生在如下代码中:
foobar = 'Al'
print('My name is ' + fooba)
或者:
spam = ruond(4.2)
或者:
spam = Round(4.2)
4)方法名拼写错误(导致 “AttributeError: 'str' object has no attribute 'lowerr'”)
该错误发生在如下代码中:
spam = 'THIS IS IN LOWERCASE.'
spam = spam.lowerr()
5)引用超过list最大索引(导致“IndexError: list index out of range”)
该错误发生在如下代码中:
spam = ['cat', 'dog', 'mouse']
print(spam[6])






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
1条评论