【Python基础】IndentationError: unexpected indent
返回: Python基础 索引页
在 python-3.7.4-embed amd64\phython.exe 启动后,在 >>> 提示符下,
粘贴拷贝官方的原始的类创建程序,会报错:
程序片段:
class Dog: def __init__(self, name): self.name = name self.tricks = [] # creates a new empty list for each dog def add_trick(self, trick): self.tricks.append(trick)
错误信息:
>>> def add_trick(self, trick): File "<stdin>", line 1 def add_trick(self, trick): ^ IndentationError: unexpected indent >>> self.tricks.append(trick) File "<stdin>", line 1 self.tricks.append(trick) ^ IndentationError: unexpected indent >>> self.tricks.append(trick)
在两个方法的空行,加入注释行 "#####" 后,可以正常工作。
class Dog: def __init__(self, name): self.name = name self.tricks = [] # creates a new empty list for each dog ############# def add_trick(self, trick): self.tricks.append(trick)
此时,拷贝后,给出提示符 "...", 再次回车,回到 ">>>" 提示符状态。
>>> class Dog: ... def __init__(self, name): ... self.name = name ... self.tricks = [] # creates a new empty list for each dog ... ############# ... def add_trick(self, trick): ... self.tricks.append(trick) ... >>>
返回: Python基础 索引页