Looi Looi - 6 months ago 11
Python Question

How to call a function from within another function

Im trying to make a program which can calculate density, terminal velocity and viscosity.
I've seperated them so that it's easier for a user to understand.

The flow is as follows:
It's pretty simple but when the density() function is called, its meant to calculate denCould someone help? The code is below, and so is the error.

import sys


def terminalvelandviscocalc(ms,ds,pa):
import math as m
masssteel=ms
diametersteel=ds
projectarea=pa
termvelo=m.sqrt((2*masssteel*9.81)/(density*projectarea*0.5))
visco=((masssteel*9.8)-((4.0/3.0)*((diametersteel/2)**3)*9.8*density))/(6*m.pi*(diametersteel/2)*termvelo)
print 'The terminal velocity is: %.2f' %termvelo
print 'The viscosity is: %.2f' %visco
return termvelo
return visco
And the error :

termvelo=m.sqrt((2*masssteel*9.81)/(density*projectarea*0.5))
TypeError: unsupported operand type(s) for *: 'function' and 'float'

Answer

density is a function, yet in the line termvelo=m.sqrt((2*masssteel*9.81)/(density*projectarea*0.5)) you use it as a variable.

You should call it with (), and provide the 3 arguments that it expects (I called them arg1, arg2, arg3. You should use the correct variable names from your code):

termvelo=m.sqrt((2*masssteel*9.81)/(density(arg1, arg2, arg3)*projectarea*0.5))