5个常见的Python错误类型!忘记在if,for,def,elif,else,class等声明末尾加 :
忘记在if,for,def,elif,else,class等声明末尾加 :
会导致“SyntaxError :invalid syntax”如下:
if spam == 42
print('Hello!')
2、
也会导致“SyntaxError: invalid syntax”
= 是赋值操作符而 == 是等于比较操作。该错误发生在如下代码中:
if spam = 42:
print('Hello!')
3、
“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”
记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中:
print('Hello!')
print('Howdy!')
或者:
if spam == 42:
print('Hello!')
print('Howdy!')
导致“SyntaxError: EOL while scanning string literal”
print(Hello!')
print('Hello!)
myName = 'Al'
print('My name is ' + myName + . How are you?')
导致“TypeError: ‘str’ object does not support item assignment”
string是一种不可变的数据类型,该错误发生在如下代码中:
spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)
原创文章,作者:修行,如若转载,请注明作者昵称:修行及出处:https://www.fjsqywlkj.top/computer/python-programming/2725.html