我有以下功能,每当我尝试运行它时都会收到Indentation Error
:
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
如何解决python中的IndentationError错误?
答案 0 :(得分: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 :(得分: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)
答案 2 :(得分:0)
这仅仅是因为语法。在while循环之后,转到新行,并给Tab键,然后开始编写如下的语句。
>>> while a < 10: *#this is your condition end with colon ':'*
... print(a) *#once come to new line press Tab button it will resolve problem*
答案 3 :(得分:0)
def fib(n):
a, b = 0, 1
while a < n:
print (a, end=' ')
a, b = b, a+b
print()
fib(1000)