Search code examples
pythonerror-handlingindentation

Can't write new lines because of "IndentationError: unexpected indent" in Python


I've read topics about this error and there were problems with wrong spaces. In my problem aren't problems with if x == 1 : I've added x = 2 below even_or_odd and it throws

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

Code

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())

Solution

  • You have bad indentation. Delete blank space before this line and then use TAB to correct indentation. Try it :)