2013-05-04 91 views
-3

我有以下的功能,我收到Indentation Error每當我嘗試運行它:縮進錯誤While循環在Python

def fib(n):  
    # write Fibonacci series up to n 
    """Print a Fibonacci series up to n.""" 
a, b = 0, 1 
while a < n: 
print a 
a, b = b, a+b 
# Now call the function we just defined: 
fib(2000) 

錯誤消息:

print a 
     ^
IndentationError: expected an indented block 

如何解決的IndentationError Python中的錯誤?

+0

歡迎使用python編程,其中縮進螺絲你。基本上你需要確保你使用一個標籤或空間,而不是兩個。 – btevfik 2013-05-04 08:23:36

+0

爲什麼這個問題有這麼多downvotes? – bakalolo 2016-03-23 21:24:09

回答

1

您需要正確縮進代碼。就像其他語言使用括號一樣,Python使用縮進:

def fib(n):  
    # write Fibonacci series up to n 
    """Print a Fibonacci series up to n.""" 
    a, b = 0, 1 

    while a < n: 
     print a 
     a, b = b, a+b 
1

您必須添加空格。你的鱈魚必須是這樣的:

def fib(n):  
# write Fibonacci series up to n 
"""Print a Fibonacci series up to n.""" 
     a, b = 0, 1 

     while a < n: 

      print a 

      a, b = b, a+b 

# Now call the function we just defined: 
fib(2000)