由于IndentationError而无法编写新行:意外缩进Python

时间:2016-03-09 18:03:31

标签: python error-handling indentation

我已经对这个错误提出了主题,并且错误的空间存在问题。 我的问题不是if x == 1 :的问题 我在even_or_odd之下添加了x = 2并抛出

Traceback (most recent call last):
  File "server.py", line 16, in <module>
    from router import RouteLayer
  File "/home/pi/whatsapp-bot-seed/src/router.py", line 13, in <module>
    from views.super_kacper import SuperKacper
  File "/home/pi/whatsapp-bot-seed/src/views/super_kacper.py", line 25
    xd = 2
    ^
IndentationError: unexpected indent

代码/

from utils.media_sender import UrlPrintSender
from yowsup.layers.protocol_messages.protocolentities.message_text import TextMessageProtocolEntity
import random

class SuperKacper():
    def __init__(self, interface_layer):
        self.interface_layer = interface_layer
        self.url_print_sender = UrlPrintSender(self.interface_layer)
        self.routes = [
            ("/(?P<evenOrOdd>even|odd)$", self.even_or_odd),
        ]

    def even_or_odd(self, message=None, match=None, to=None):
        is_odd = len(match.group("evenOrOdd")) % 2
        test = 2 #<<<<<<<<<<When I add someting, here test = 2 
        num = random.randint(1, 10)
        if (is_odd and num % 2) or (not is_odd and not num % 2):
            return TextMessageProtocolEntity("[%d]\nYou win." % num, to=message.getFrom())
        else:
            return TextMessageProtocolEntity("[%d]\nYou lose!" % num, to=message.getFrom())

1 个答案:

答案 0 :(得分:0)

你有不好的缩进,删除此行之前的空格,然后使用TAB来纠正缩进,试试吧:)