Indentation problem

I have no idea why I’m getting an indentation error on line 3.YES, I did use spaces instead of tabs and still redid the whole indentation.When running nosetests, I get an error saying “Indentation error:Unexpected indent”.PLEASE HELP.

class lexicon(object):
    directions=("north","south","west","east","left","right","forward","backward")
    verbs=("go","eat","punch","kill","fight","greet")
    stop=("the","in","of","is")
    noun=("bear","princess","cat","dog")
    number=("1234","3","91234")
    error=("ASDFASDFASDF","IAS")

    def __init__(self,sentence):
        lowered=sentence.lower()
        splitted=lowered.split()
        if splitted in directions:
            return ("directions",splitted)
        if splitted in verbs:
		    return ("verbs",splitted)
        if splitted in stop:
		    return ("stop",splitted)
        if splitted in noun:
		    return ("noun",splitted)
        if splitted in number:
		    return ("number",splitted)
        if splitted in error:
		    return ("error",splitted)

    def scan(self,sentence)
	    YAY=sentence.__init__()
		return YAY

@b1lal545
hello
I copied your code into visual studio code and got an error on line 15, I deleted spaces/tabs then re-tabbed and aligned, it now appears to work

class lexicon(object):
    directions=("north","south","west","east","left","right","forward","backward")
    verbs=("go","eat","punch","kill","fight","greet")
    stop=("the","in","of","is")
    noun=("bear","princess","cat","dog")
    number=("1234","3","91234")
    error=("ASDFASDFASDF","IAS")
    
    def __init__(self,sentence):
        lowered=sentence.lower()
        splitted=lowered.split()
        if splitted in directions:
            return ("directions",splitted)
        if splitted in verbs:
            return ("verbs",splitted)
        if splitted in stop:
		    return ("stop",splitted)
        if splitted in noun:
		    return ("noun",splitted)
        if splitted in number:
		    return ("number",splitted)
        if splitted in error:
		    return ("error",splitted)

    def scan(self,sentence)
	    YAY=sentence.__init__()
		return YAY

regards
jason

Thank you for your help. May I know what the error on line 15 was?Also, if the only thing you did was retab the lines like I did, I.E, you just deleted the spaces before each line in the class and then filled them up with 4 or 8 spaces again;then why did your’s start working when my solution didn’t? Do you have any idea?

@b1lal545
hello
this is the error:

File "~\lpthw\indent_problem.py", line 15
    return ("verbs",splitted)
                            ^
TabError: inconsistent use of tabs and spaces in indentation

to get the error I copied your original code over the repaired code and noticed no difference in alignment, it all looked fine then got the above error, so guess there must be a mix of spaces and tabs (as the error says lol) which Python is not happy about.

I got the code working by deleting what appeared to be tabs before each ‘return’ line then reinserted the required tabs to correct indenting and inserted a missing ‘:’
jason

1 Like

Thanks man.I will now exclusively use spaces and set the use spaces instead of tabs option in my text editor.(Notepad++ also sublime text 3)

@b1lal545
when you said exclusively use spaces, I was thinking why, that’s slower then tabs and I’m lazy, 3 extra keystrokes per indentation compared to a single for a tab, so I googled it and found

thanks for opening my eyes
have a good day coding
jason

1 Like

OK I am legitimately baffled now.I ran your correctly indented code and replaced my lexicon with this (ex47) and ran nosetests. And it still told me that there was an indentation error on line 3… I don’t know what to do anymore.

C:\Python27\projects\Lexicon>nosetests
E

ERROR: Failure: IndentationError (unexpected indent (lexicons.py, line 3))

Traceback (most recent call last):
File “C:\Python27\lib\site-packages\nose\loader.py”, line 418, in loadTestsFromName
addr.filename, addr.module)
File “C:\Python27\lib\site-packages\nose\importer.py”, line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File “C:\Python27\lib\site-packages\nose\importer.py”, line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File “C:\Python27\projects\Lexicon\tests\lexicon_tests.py”, line 2, in
from lexicon import lexicons
File “C:\Python27\projects\Lexicon\lexicon\lexicons.py”, line 3
verbs=(“go”,“eat”,“punch”,“kill”,“fight”,“greet”)
^
IndentationError: unexpected indent


Ran 1 test in 0.007s

FAILED (errors=1)

@b1lal545
Hello
I can’t see your code therefore I cannot comment on the spacing, your further ahead then me, well done.
The error says:

verbs=(“go”,“eat”,“punch”,“kill”,“fight”,“greet”)
^
IndentationError: unexpected indent

can you just check this line, remove all spacing to reset it then space it again, if not post your code and I’ll take a look.
Regards
Jason

Dude it was the exact same code you corrected which I then copy pasted into my own but I still got the exact same error.

class lexicon(object):
    directions=("north","south","west","east","left","right","forward","backward")
	verbs=("go","eat","punch","kill","fight","greet")
	stop=("the","in","of","is")
	noun=("bear","princess","cat","dog")
	number=("1234","3","91234")
	error=("ASDFASDFASDF","IAS")
		
	
    def __init__(self,sentence):
        soon=sentence.lower()
		toon=soon.split()
		if toon in directions:
		    return ("directions",toon)
		if toon in verbs:
		    return ("verbs",toon)
		if toon in stop:
		    return ("stop",toon)
		if toon in noun:
		    return ("noun",toon)
		if toon in number:
		    return ("number",toon)
		if toon in error:
		    return ("error",toon)
		
		
    def scan(self,sentence)
	    YAY=sentence.__init__()
		return YAY
	    
		

poon=lexicon(sentence)

@b1lal545
Hello

the lines starting with ‘soon’, ‘toon’ and ‘if’ are not lined up in your post, you have to many spaces on the ‘if’ and ‘return’ lines you have an extra set of 4 spaces that are not required. The only way that jumped to mind to show was if you paste the code into Libre office writer (or similar) and toggle the formatting marks buttons your will see all the spaces as dots, and I saw there was to many, so back to Visual studio code I went, deleted the leading spaces on the lines I was getting an error and reinserted tabs (you can use spaces) and your code then ran as far as the missing ‘:’ on def scan(etc.

am I making sense?
Jason


class lexicon(object):

    directions=("north","south","west","east","left","right","forward","backward")
    verbs=("go","eat","punch","kill","fight","greet")
    stop=("the","in","of","is")
    noun=("bear","princess","cat","dog")
    number=("1234","3","91234")
    error=("ASDFASDFASDF","IAS")
                
        
    def __init__(self,sentence):
        soon=sentence.lower()
        toon=soon.split()
        if toon in directions:
            return ("directions",toon)
        if toon in verbs:
            return ("verbs",toon)
        if toon in stop:
            return ("stop",toon)
        if toon in noun:
            return ("noun",toon)
        if toon in number:
            return ("number",toon)
        if toon in error:
            return ("error",toon)
                
                
    def scan(self,sentence):
        YAY=sentence.__init__()
        return YAY
            
                

poon=lexicon(sentence)
1 Like

Thanks man, I’ll stop bothering you now.runs away

1 Like

As a side note @b1lal545, your implementation of the scanner is going to have problems. I used a dict to map the words rather than an if-statement. You’ll want to think about this in the reverse of what you have here. Think, “If I’m given a word, I do mywords.get(word) to map the word to its type.”