Home Ask Login Register

Developers Planet

Your answer is one click away!

Michelle Arroway February 2016

Unexpected indente on line 5?

I just started learning to programme with Python. I have just tried to use the if elif and else statement but my computation kept telling me there is an indentation error.

I am quite confused as this is a really simple programme. It seems like when I put more than 2 lines of code after the if statement, there will be a problem. Could you please help me with this?

Thank you soooo much

x = raw_input ('Give me a number ')

if x > 100: 
    print 'Big'
    print 'number'
    print 'hahaha'
elif x == 100:
    print 'yeah'
    print '...' 


Jan Vlcinsky February 2016

It is very likely, you have tabs in your text.

It is recommended to set up your editor not to use tabs in Python code and possibly convert them to spaces before saving.

Even without such editor configuration, you can fix your code by searching for tabs (you may let it show somehow) and replacing by spaces.

user2357112 February 2016

You've mixed tabs and spaces. Unlike your text editor, Python 2 treats tabs like Notepad, as enough spaces to reach the next 8-space indentation level. Python 3 treats tabs and spaces as never equivalent.

Turn on "show whitespace" in your editor to see it, and run Python with the -tt flag to make it tell you about mixed tabs and spaces. Your editor may also have a "convert tabs to spaces" tool to fix the problem.

Michal Frystacky February 2016

A couple of suggestions:

The error sounds like you aren't using whitespace properly make sure every line has consistent spacing (don't mix tabs and whitespace) (like in your question)

also you might wish to modify your code as shown below:

if int(x) > 100: #got to tell python to convert x (a string, str into int)
    print 'Big'
    print 'number'
    print 'hahaha'
elif int(x) == 100:
    print 'yeah'

Post Status

Asked in February 2016
Viewed 3,040 times
Voted 14
Answered 3 times


Leave an answer

Quote of the day: live life