简体   繁体   中英

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

If I print the value that the function returns, I obtain: function alpha at 0x05870630

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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