专栏名称: 36大数据
关注大数据和互联网趋势,最大,最权威,最干货的大数据微信号(dashuju36)。大数据第一科技媒体。不发软文,只做知识分享。
目录
相关文章推荐
51好读  ›  专栏  ›  36大数据

干货 | 17个新手常见的Python运行时错误

36大数据  · 公众号  · 大数据  · 2017-09-15 07:50

正文

请到「今天看啥」查看全文



该错误发生在如下代码中:

numEggs = 12print('I have ' + numEggs + ' eggs.')

而你实际想要这样做:

numEggs = 12print('I have ' + str(numEggs) + ' eggs.')

或者:

numEggs = 12print('I have %s eggs.' % (numEggs))

7

在字符串首尾忘记加引号(导致“SyntaxError: EOL while scanning string literal”)

该错误发生在如下代码中:

print(Hello!')

或者:

print('Hello!)

或者:

myName = 'Al'print('My name is ' + myName + . How are you?')

8

变量或者函数名拼写错误(导致“NameError: name ‘fooba’ is not defined”)

该错误发生在如下代码中:

foobar = 'Al'print('My name is ' + fooba)

或者:

spam = ruond(4.2)

或者:

spam = Round(4.2)

9

方法名拼写错误(导致 “AttributeError: ‘str’ object has no attribute ‘lowerr‘”)

该错误发生在如下代码中:

spam = 'THIS IS IN LOWERCASE.'spam = spam.lowerr()

10

引用超过list最大索引(导致“IndexError: list index out of range”)

该错误发生在如下代码中:

spam = ['cat', 'dog', 'mouse']
print(spam[6])

11

使用不存在的字典键值(导致“KeyError:‘spam’”)

该错误发生在如下代码中:

spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'}
print('The name of my pet zebra is ' + spam['zebra'])

12

尝试使用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







请到「今天看啥」查看全文