A python program to take input and sqaure it using lambda function shows None as result. What am I doing wrong?

Question:

print("Problem Statement: Accept a parameter and return its square")
print()

squareX = lambda num : num * num

def sq(squareX, num):
    result = squareX(num)

def main():
    print("Please enter the number to find its square:")
    value = int(input())
    print()

    Ans = sq(squareX, value)

    print("The square of number is:", Ans)

if __name__ == "__main__":
    main()

I’m trying to write a program that utilizes function calling and lambda. It mostly works but I’m not able to retrieve the square value and the result "Ans" is always "None". Not able to figure out why it is happening. Any help is appreciated.

Asked By: nitin kashyap

||

Answers:

You forgot the return statement in sq. So the correct code is:

print("Problem Statement: Accept a parameter and return its square")
print()

squareX = lambda num : num * num

def sq(squareX, num):
    result = squareX(num)
    return result

def main():
    print("Please enter the number to find its square:")
    value = int(input())
    print()

    Ans = sq(squareX, value)

    print("The square of number is:", Ans)

if __name__ == "__main__":
    main()
Answered By: Zitronenenlolli
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.