How to multiply two functions in python
Question:
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)
Answers:
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:
return float(input(f'{s}: '))
except:
print('Invaid, try again')
prompt('Length') * prompt('Width')
Output:
Length: 2
Width: 3
6.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)
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:
return float(input(f'{s}: '))
except:
print('Invaid, try again')
prompt('Length') * prompt('Width')
Output:
Length: 2
Width: 3
6.0