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

Viewed 26

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

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