作为 python 的初学者,我不断收到缩进错误,但我不知道为什么?
>>> gbp_interest = 0.5
>>>
>>> difference_between_pairs = us_interest - gbp_interest
>>> print(abs(difference_between_pairs))
0.3
>>>
>>> if abs(difference_between_pairs) > 0.1 :
... print(max(us_interest, gbp_interest))
File "<stdin>", line 2
print(max(us_interest, gbp_interest))
^
IndentationError: expected an indented block
此外,Python 对缩进敏感(比其他编程语言更敏感)是否有原因?
我想说,缩进是 python 的关键特性。
缩进可以帮助您定义代码块。它对于 Python 来说就像
{ }
通常对于其他一些编程语言一样。
缩进您的
print
:
>>> if abs(difference_between_pairs) > 0.1 :
... print(max(us_interest, gbp_interest))
考虑开始一个教程,网络上有大量资源。 示例:https://docs.python.org/3/tutorial/index.html