Casper2017
Posts: 56
Joined: Thu Mar 23, 2017 12:44 pm

What to do with “Unexpected indent” in python 3? "while True:

Wed Sep 13, 2017 10:13 am

Hi

When writing a simple program to a complex program and you know it works fine, when a "while True: is placed in the program so it will repeat
all one gets is Unexpected Indent, this totally ruins the program.

I could give several program examples, the trouble is python 3 seems to be showing this message all the time.

It take hours of playing around with the line code to have it up and working.

Is there any way of stopping this "unexpected incident " message from occurring.


Thanks :mrgreen: :mrgreen:

scotty101
Posts: 3680
Joined: Fri Jun 08, 2012 6:03 pm

Re: What to do with “Unexpected indent” in python 3? "while True:

Wed Sep 13, 2017 10:20 am

This problem is usually caused because of different indentation styles.

Some people (who are wrong*) use tabs.
Some people (who are correct*) use spaces

Python 3 disallows the use of both tabs and spaces.
The solution is to go through your code and replace all tabs with spaces. A good text editor like Geany will help you to do this as it can automatically replace tabs with spaces and can be configured to enter a specified number of spaces rather than a tab when you press the tab key.

* See PEP-8 for recommended python indentation style.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
paddyg
Posts: 2337
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: What to do with “Unexpected indent” in python 3? "while True:

Wed Sep 13, 2017 10:25 am

In my experience this has 99.9% been due to mixed tabs and spaces, either with multiple people working on a project or me copying some useful code from somewhere else. To start with I was fairly tabspace agnostic but I now always use spaces and set the editor (geany) to show whitespace and formatting guides. It is one of the most annoying things about python (but also a USP)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

gordon77
Posts: 4116
Joined: Sun Aug 05, 2012 3:12 pm

Re: What to do with “Unexpected indent” in python 3? "while True:

Wed Sep 13, 2017 11:51 am

Casper2017 wrote:
Wed Sep 13, 2017 10:13 am
Hi

When writing a simple program to a complex program and you know it works fine, when a "while True: is placed in the program so it will repeat
all one gets is Unexpected Indent, this totally ruins the program.

I could give several program examples, the trouble is python 3 seems to be showing this message all the time.

It take hours of playing around with the line code to have it up and working.

Is there any way of stopping this "unexpected incident " message from occurring.


Thanks :mrgreen: :mrgreen:
Just be consistent with the number of indent spaces you use.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: What to do with “Unexpected indent” in python 3? "while True:

Thu Sep 14, 2017 2:32 pm

I have had this problem when using a file edited in window. if you are using window to edit try edit it through Geany or python IDLE.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

DirkS
Posts: 9902
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: What to do with “Unexpected indent” in python 3? "while True:

Thu Sep 14, 2017 5:28 pm

klintkrossa wrote:
Thu Sep 14, 2017 2:32 pm
I have had this problem when using a file edited in window. if you are using window to edit try edit it through Geany or python IDLE.
There's nothing Windows specific about it.
It depends on your editor settings. Any halfway decent editor (Windows or other) will let you set how you want to deal with the tab key and leading indents

User avatar
bensimmo
Posts: 4155
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: What to do with “Unexpected indent” in python 3? "while True:

Thu Sep 14, 2017 7:37 pm

DirkS wrote:
Thu Sep 14, 2017 5:28 pm
klintkrossa wrote:
Thu Sep 14, 2017 2:32 pm
I have had this problem when using a file edited in window. if you are using window to edit try edit it through Geany or python IDLE.
There's nothing Windows specific about it.
It depends on your editor settings. Any halfway decent editor (Windows or other) will let you set how you want to deal with the tab key and leading indents
He could be meaning a the Terminal/command line front eh GUI and something like nano
He might not be though.

It is a pain, but I've found the editors options sort it out tend to work but with a bit of editting to realign if it out of place.

Casper2017
Posts: 56
Joined: Thu Mar 23, 2017 12:44 pm

Re: What to do with “Unexpected indent” in python 3? "while True:

Sat Sep 16, 2017 9:43 am

Thank you.

That helps, several problems solved. :D

Return to “Python”