6

I have a simple piece of code that I'm not understanding where my error is coming from. The parser is barking at me with an Unexpected Indent on line 5 (the if statement). Does anyone see the problem here? I don't.

def gen_fibs():
    a, b = 0, 1
    while True:
        a, b = b, a + b
        if len(str(a)) == 1000:
            return a
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
tjsimmons
  • 733
  • 4
  • 10
  • 21

5 Answers5

13

If you just copy+pasted your code, then you used a tab on the line with the if statement. Python interprets a tab as 8 spaces and not 4. Don't ever use tabs with python1 :)

1 Or at least don't ever use tabs and spaces mixed. It's highly advisable to use 4 spaces for consistency with the rest of the python universe.

aaronasterling
  • 68,820
  • 20
  • 127
  • 125
  • Ahh! I copy and pasted this code from other code I had. Good grief. I commented on an answer above, but I didn't realize what had caused it until your answer. Thanks! – tjsimmons Nov 24 '10 at 04:39
  • 1
    Back at zero now ;) I was about to say the same thing. I would recommend running a search and replace on `\t` characters and replacing them with `[four spaces]`. – Blender Nov 24 '10 at 04:42
  • Because you decided to pontificate on a holy war instead of simply answering the question. – Nicholas Knight Nov 24 '10 at 04:44
  • 2
    @Nicholas Knight. There's no holy war here. The issue is long over and decided by PEP-8. Hope that clears up your obvious confusion :) – aaronasterling Nov 24 '10 at 04:46
  • 1
    PEP-8 is a style guide, not a commandment, and not everyone agrees with all of it. Lose the fundamentalism. – Nicholas Knight Nov 24 '10 at 04:53
  • @Nicholas Knight - Given that you're the one downvoting over a 'holy war', I wouldn't be so quick to drop the f-bomb. Most python projects that I have __Ever Looked At__ follow this convention. It is pretty standard. A better example of an oft-ignored part of PEP-8 is `under_score` vs `camelCase`. Please compose your rebuttal in Emacs. – aaronasterling Nov 24 '10 at 04:59
  • If you'd said "don't ever use spaces with python", I would have downvoted, too. I'm reacting to fundamentalism, not the side it's on. Incidentally, most C projects I have **Ever Looked At** use tabs to indent, but I would never think for an instant to tell a C programmer "don't ever use spaces with C". – Nicholas Knight Nov 24 '10 at 05:05
4

You're mixing tabs and spaces. Tabs are always considered the same as 8 spaces for the purposes of indenting. Run the script with python -tt to verify.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Check if you aren't mixing tabs with spaces or something, because your code pasted verbatim doesn't produce any errors.

Marek Sapota
  • 20,103
  • 3
  • 34
  • 47
  • 1
    Yeah, this was it. I use TextMate on the Mac to write my code, and I use the tab key to indent (it puts in the 4 spaces). In the editor it looked fine, but when I pasted it in here, there was an extra tab on the if statement. I switched it to a manual four spaces and it worked. Bizarre. – tjsimmons Nov 24 '10 at 04:38
  • 1
    You can set up TextMate to always use spaces in .py files. – Russell Borogove Nov 24 '10 at 06:54
1

The line with a, b = b, a + b is indented with 8 spaces, and the if line is indented with 4 spaces and a tab. Configure your editor to never ever insert tabs ever.

(Python considers a tab to be 8 spaces, but it's easier just not to use them)

luqui
  • 59,485
  • 12
  • 145
  • 204
-1

Check whether your whitespace in front of every line is correctly typed. You may have typed tabs instead of spaces - try deleting all the space and change it so you use only tabs or only spaces.

icyrock.com
  • 27,952
  • 4
  • 66
  • 85