Home > Net >  A python program to take input and sqaure it using lambda function shows None as result. What am I d
A python program to take input and sqaure it using lambda function shows None as result. What am I d

Time:10-30

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.

CodePudding user response:

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