开发者

Unexpected Indent error in Python [duplicate]

This question already has answers here: I'm getting an IndentationError. How do I fix it? (5 answers) Closed 3 years ago.

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


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.


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.


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


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜