cde*_*ola 1 python function syntax-error
我是一个初学python程序员,但已编写了几个脚本,其中包括我定义自己的函数并使用它们的脚本.我似乎无法在IDLE中使用任何用户定义的函数.想知道我是不是疯了/笨.有人可以解释一下以下结果吗?谢谢:
def f(x,y):
solution = x+y
return solution
f(2,2)
SyntaxError: invalid syntax
>>> a = f(2,2)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
a = f(2,2)
NameError: name 'f' is not defined
Run Code Online (Sandbox Code Playgroud)
def g(x):
solution = x + 2
return solution
g(2)
SyntaxError: invalid syntax
>>> a = g(2)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
a = g(2)
NameError: name 'g' is not defined
Run Code Online (Sandbox Code Playgroud)
在函数定义之后添加一个空行,以使解释器理解它已完成.
>>> def f(x,y):
solution = x+y
return solution
>>> f(2,2)
4
Run Code Online (Sandbox Code Playgroud)