2013-05-04 44 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)