user14223968
user14223968

Reputation: 21

Unexpected indent and other errors

class Pacco:
    STATES = ('Il pacco e` stato ordinato ma non ancora spedito',
              'Il pacco e` stato spedito ma non ancora ricevuto',
              'Il pacco e` stato ricevuto')
    indice = 0
    
    def __init__(self):
        self.state = Pacco.STATES[0]
        self._succ = Pacco.STATES[1]
        self._prec = None

    def next(self):
        print(self.state)

    @property
    def state(self):
        Pacco.indice += 1
        self.state = Pacco.STATES[Pacco.indice]
        self._succ = Pacco.STATES[Pacco.indice+1]
        self._prec = Pacco.STATES[Pacco.indice-1]

At indice = 0 it gives me the error Unexpected indent and at the line Pacco.indice += 1 it gives me the error

Unindent does not match any outer indentation level

And at the next 3 lines it gives the errors

Unresolved reference 'self'...

Can someone tell me why?

Upvotes: 0

Views: 58

Answers (3)

AkIonSight
AkIonSight

Reputation: 385

you can use the autopep8

pip install autopep8

now type autopep8 <yourfile.py> in the terminal and enter

see that code will now be formatted in the right way

Upvotes: 0

neuops
neuops

Reputation: 352

First of all, keep all of your predefined variables inside your __init__ function. Second, the self.state variable needs to be self._state because there is a function with the same name. Third, refer to @wjandrea's answer for his advice on tabs and space formatting. The code should be like this:

class Pacco:
    
    def __init__(self):
        self.STATES = ('Il pacco e` stato ordinato ma non ancora spedito',
                       'Il pacco e` stato spedito ma non ancora ricevuto',
                       'Il pacco e` stato ricevuto')
        self.indice = 0
        self._state = self.STATES[0]
        self._succ = self.STATES[1]
        self._prec = None

    def next(self):
        print(self.state)

    @property
    def state(self):
        self.indice += 1
        self._state = self.STATES[self.indice]
        self._succ = self.STATES[self.indice+1]
        self._prec = self.STATES[self.indice-1]

Upvotes: 0

wjandrea
wjandrea

Reputation: 32963

You've mixed tabs and spaces. Convert all the tabs to spaces (per PEP 8), and the syntax works fine.

Note: The Stack Overflow formatter converts tabs to four spaces, so you can't see it in the post, but you can if you check the source.

Upvotes: 1

Related Questions