简体   繁体   English

在函数中打印返回值,Python

[英]Printing return value in function, Python

When I print the value that my function returns, it has strange characters. 当我打印函数返回的值时,它具有奇怪的字符。

If I print the value that I care inside the function, I obtain the right value: 0.653594771242 如果在函数中打印我关心的值,则将获得正确的值:0.653594771242

If I print the value that the function returns, I obtain: function alpha at 0x05870630 如果我打印函数返回的值,则会获得:0x05870630处的函数alpha

def alpha(v1,v2):

    a=(v1,v2)
    b=1/sum(a)
    print(b)
    return b

alpha(0.817,0.713)

print(alpha)

This is because you are printing an object ie a function. 这是因为要打印对象即功能。 Functions in python are objects. python中的函数是对象。

If you want to print the value returned by the function then this may help you. 如果要打印函数返回的值,则可能会有所帮助。

print(alpha(0.817,0.713))

This way would probably make more sense to you. 这样对您可能更有意义。

def alpha(v1,v2):

    a=(v1,v2)
    b=1/sum(a)
    print(b)
    return b

result = alpha(0.817,0.713)

print(result)

This way the function is returning the value to result then you are simply printing the result. 这样,函数将返回结果值,然后您只需打印结果即可。

代替print(alpha),您需要编写print(alpha(0.817,0.713))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM