[Solved] Squaring the odd number and squaring the number-1 if number is even [closed]


Here is easy sample code to understand:

def odd_square(number):
    if number == 0:
        return 0
    elif number % 2 == 1:
        return number*number
    else:
        return (number - 1)*(number - 1)

square = odd_square(int(input("Enter number to square: ")))
print("square is: ",square)

you need to convert number to integer as input() returns string.

TypeError: odd_square() takes 0 positional arguments but 1 was given

Your odd_square() function don’t take any argument as per your def. It seems you are providing argument to odd_square() while calling it.

Comment below if you find it difficult to understand.

3

solved Squaring the odd number and squaring the number-1 if number is even [closed]