IndentationError: unexpected indent with web.py
IndentationError: unexpected indent with web.py
我在 app.py 文件中有这段代码
import web
urls = (
'/hello', 'Index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(name="Nobody", greet="Hello")
greeting = "%s" % (form.menu)
if greeting == "ct":
return render.ct(greeting=greeting)
elif greeting == "vjc":
return render.vjc(greeting=greeting)
elif greeting == "sa":
return render.sa(greeting=greeting)
elif greeting == "wc":
return render.wc(greeting=greeting)
elif greeting == "pt":
return render.pt(greeting=greeting)
else:
return render.s(greeting=greeting)
if __name__ == "__main__":
app.run()
当我在 cmd 中启动服务器时,出现以下意外缩进错误
$ python bin/app.py
File "bin/app.py", line 18
if greeting == "ct":
^
IndentationError: unexpected indent
谁能告诉我这个错误的原因。我想我已经多次检查了缩进,我没有发现代码有任何问题。
提前感谢大家的帮助!
您混合使用制表符和空格进行缩进。 if
行使用制表符,但前一行只有空格:
>>> lines = '''\
... greeting = "%s" % (form.menu)
... if greeting == "ct":
... '''
>>> lines
' greeting = "%s" % (form.menu)\n\t\tif greeting == "ct":\n'
>>> # ^^^ is all spaces, while here ^^^^ you use two tabs.
...
Python 将制表符扩展到每 8 列,但您可能将编辑器设置为仅对制表符使用 4 个空格,这进一步增加了混乱。您的 greeting
行缩进为 8 个空格,而下一行的两个制表符扩展为 16 个空格。
永远不要使用混合缩进样式;坚持只使用制表符或只使用空格。
您可以将大多数编辑器配置为使用空格 仅 进行缩进;这是 Python styleguide (PEP 8) 推荐的内容:
Tabs or Spaces?
Never mix tabs and spaces.
The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.
我在 app.py 文件中有这段代码
import web
urls = (
'/hello', 'Index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(name="Nobody", greet="Hello")
greeting = "%s" % (form.menu)
if greeting == "ct":
return render.ct(greeting=greeting)
elif greeting == "vjc":
return render.vjc(greeting=greeting)
elif greeting == "sa":
return render.sa(greeting=greeting)
elif greeting == "wc":
return render.wc(greeting=greeting)
elif greeting == "pt":
return render.pt(greeting=greeting)
else:
return render.s(greeting=greeting)
if __name__ == "__main__":
app.run()
当我在 cmd 中启动服务器时,出现以下意外缩进错误
$ python bin/app.py
File "bin/app.py", line 18
if greeting == "ct":
^
IndentationError: unexpected indent
谁能告诉我这个错误的原因。我想我已经多次检查了缩进,我没有发现代码有任何问题。
提前感谢大家的帮助!
您混合使用制表符和空格进行缩进。 if
行使用制表符,但前一行只有空格:
>>> lines = '''\
... greeting = "%s" % (form.menu)
... if greeting == "ct":
... '''
>>> lines
' greeting = "%s" % (form.menu)\n\t\tif greeting == "ct":\n'
>>> # ^^^ is all spaces, while here ^^^^ you use two tabs.
...
Python 将制表符扩展到每 8 列,但您可能将编辑器设置为仅对制表符使用 4 个空格,这进一步增加了混乱。您的 greeting
行缩进为 8 个空格,而下一行的两个制表符扩展为 16 个空格。
永远不要使用混合缩进样式;坚持只使用制表符或只使用空格。
您可以将大多数编辑器配置为使用空格 仅 进行缩进;这是 Python styleguide (PEP 8) 推荐的内容:
Tabs or Spaces?
Never mix tabs and spaces.
The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.