函数参数混淆
Confusion as to function parameters
我正在尝试制作一个接受 2 个参数的简单函数,并使用“+”将它们相加。
def do_plus (a,b):
a=3
b=7
result = a + b
print (result)
然而,我没有得到任何返回值,函数被执行但没有显示输出。
您缺少缩进。
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)
您可能希望将逻辑与 input/output 分开,这样:
def do_plus(a, b):
result = a + b
return result
res = do_plus(3, 7)
print(res)
很难从您的代码中分辨出来,因为缩进已关闭,但是一个简单的加法函数可以是这样的:
def addition(a, b):
return a + b
您正在接受参数 a
和 b
,但随后为它们赋值 7
和 3
,因此无论如何,它都会 return 10
.
试试这个:
def do_plus (a,b):
print=a+b
do_plus(3, 7)
您可以调用函数 "do_plus" 传递参数并打印函数 return
注意结果前的 "spaces" 在 python 脚本的识别中很重要
我正在尝试制作一个接受 2 个参数的简单函数,并使用“+”将它们相加。
def do_plus (a,b):
a=3
b=7
result = a + b
print (result)
然而,我没有得到任何返回值,函数被执行但没有显示输出。
您缺少缩进。
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)
您可能希望将逻辑与 input/output 分开,这样:
def do_plus(a, b):
result = a + b
return result
res = do_plus(3, 7)
print(res)
很难从您的代码中分辨出来,因为缩进已关闭,但是一个简单的加法函数可以是这样的:
def addition(a, b):
return a + b
您正在接受参数 a
和 b
,但随后为它们赋值 7
和 3
,因此无论如何,它都会 return 10
.
试试这个:
def do_plus (a,b):
print=a+b
do_plus(3, 7)
您可以调用函数 "do_plus" 传递参数并打印函数 return
注意结果前的 "spaces" 在 python 脚本的识别中很重要