0

I'm writing code for find parameter and area of different 2D objects. I decide to use local functions for this or the code become deep nested code. when I run this code it's says "TypeError: unsupported operand type(s) for *: 'function' and 'function'"

#Area Of Rectangle Section
def rectanglearea(a, b):
    length = a
    width = b
    #area = length * width
    print ("")
    print ("Formula = Length X Width")
    #print ("Area Of This Rectangle Is: ", area)
#Length local function
def length (a):
    length = input ("Input Length: ")
    lengthLoop = 1
    while lengthLoop == 1:
        try :
            length = float (length)
            break
        except:
            print ("Invalid input please TRYT AGAIN !!")
            continue
#Width local function
def width (a):
    width = input ("Input Width: ")
    widthLoop = 1
    while widthLoop == 1:
        try :
            width = float(width)
            break
        except :
            print ("Invalid input please TRY AGAIN !!!")
                #Length
                sampleValueForLength = 1
                length (sampleValueForLength)
                    
                #Width       
                sampleValueForWidth = 1
                width (sampleValueForWidth)
                    
                rectanglearea (length, width)

i tried like this :

#Area Of Rectangle Section
def rectanglearea(a, b):
    length = a
    width = b
    **area = length (a) * width (a)**
    print ("")
    print ("Formula = Length X Width")
    print ("Area Of This Rectangle Is: ", area)
2
  • 4
    sorry, but this code is a mess. length() and width() don't return any values, don't use the parameter that is passed to them, use the function name as a variable name, probably other problems Commented Nov 7, 2022 at 17:00
  • You don't return a value from the functions length and width. Additionally you overwrite those functions with the values a and b. Commented Nov 7, 2022 at 17:01

1 Answer 1

1

You just need your function(s) to return values and be careful with your naming so you don't use the same name for variables and functions which will only confuse you if not the compiler.

Here is simple version to demonstrate:

def prompt(s):
    while True:
        try:
            w = float(input(f'{s}: '))
            return w
        except:
            print('Invaid, try again')

            
prompt('Length') * prompt('Width')
Length: 2
Width: 3
6.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.