我正在通过书中给出的例子来学习python这里是我在终端上输入的一个例子
user@ubuntu:~$ python
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def square(x):
... 'Calculates the square of the number x'
... return x*x
按下输入上面的代码后会出现以下错误
File "<stdin>", line 3
return x*x
^
IndentationError: unexpected indent
我在终端输入了这个,我不确定还需要缩进什么, 基本上书中给出的例子涉及在python中使用doc字符串,即解释函数的字符串,如果上面的代码将在我的下一步工作,将验证是否可以按照书中的说明访问doc字符串
>>> square.__doc__
'Calculates the square of the number x.'
或验证在交互式解释器上使用帮助功能
>>> help(square)
Help on function square in module __main__:
我在一个互动翻译(终端)练习这个,让我知道我上面做的错误是什么?
答案 0 :(得分:1)
return
语句之前有一个额外的空格。