简体   繁体   中英

My code is running fine but I don't know why it shows 16 instead of (16, 2)?

The answer always comes out to be 16 but what happens to the x in function sq(func,x) because func turned into 16 but x remains 2. So it should show something like 16, 2 when I try to print it.

def sq(func,x):
    y = x**2
    return func(y)

def f(x):
    return x**2

Calc = sq(f,2)
print(Calc)

Your print statement prints cal, which is the number returned by the function f(x), which is what is returned by the function sq(f,y).

If you want to print the structure that you need, try to do the following:

def sq(func,x):
    y = x**2
    return (func(y), x)

def f(x):
    return x**2

Calc = sq(f,2)
print(Calc)

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