正文
该错误发生在如下代码中:
numEggs = 12print('I have ' + numEggs + ' eggs.')
而你实际想要这样做:
numEggs = 12print('I have ' + str(numEggs) + ' eggs.')
或者:
numEggs = 12print('I have %s eggs.' % (numEggs))
在字符串首尾忘记加引号(导致“SyntaxError: EOL while scanning string literal”)
该错误发生在如下代码中:
print(Hello!')
或者:
print('Hello!)
或者:
myName = 'Al'print('My name is ' + myName + . How are you?')
变量或者函数名拼写错误(导致“NameError: name ‘fooba’ is not defined”)
该错误发生在如下代码中:
foobar = 'Al'print('My name is ' + fooba)
或者:
spam = ruond(4.2)
或者:
spam = Round(4.2)
方法名拼写错误(导致 “AttributeError: ‘str’ object has no attribute ‘lowerr‘”)
该错误发生在如下代码中:
spam = 'THIS IS IN LOWERCASE.'spam = spam.lowerr()
引用超过list最大索引(导致“IndexError: list index out of range”)
该错误发生在如下代码中:
spam = ['cat', 'dog', 'mouse']
print(spam[6])
使用不存在的字典键值(导致“KeyError:‘spam’”)
该错误发生在如下代码中:
spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'}
print('The name of my pet zebra is ' + spam['zebra'])
尝试使用Python关键字作为变量名(导致“SyntaxError:invalid syntax”)
Python关键不能用作变量名,该错误发生在如下代码中:
class = 'algebra'
Python3的关键字有:and, 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