关于功能参数的困惑

时间:2016-12-09 13:43:58

标签: python function scope interactive

我正在尝试创建一个接受2个参数的简单函数,并使用" +"将它们组合在一起。

def do_plus (a,b):
    a=3
    b=7
    result = a + b
    print (result)

但是,我没有返回任何值,执行该函数但没有显示输出。

3 个答案:

答案 0 :(得分:2)

你错过了缩进。

a=3
b=7
def do_plus (a,b):
    result =a+b
    print (result)
# and you have to call the function:
do_plus(a,b)

您可能希望将逻辑与输入/输出分开,如下所示:

def do_plus(a, b):
    result = a + b
    return result

res = do_plus(3, 7)
print(res)

答案 1 :(得分:2)

试试这个:

def do_plus (a,b):
    print=a+b
do_plus(3, 7)

你可以打电话给你的功能" do_plus"传递参数并打印wath函数返回

注意"空间"之前结果在python中重要的是脚本的标识

答案 2 :(得分:1)

很难从代码中分辨出因为缩进是关闭的,但是简单的添加功能可能是这样的:

def addition(a, b):
  return a + b

您接受参数ab,但随后为其分配值73,这样无论如何,它都会返回10