Looi Looi - 2 years ago 79
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
print 'The terminal velocity is: %.2f' %termvelo
print 'The viscosity is: %.2f' %visco
return termvelo
return visco
And the error :

TypeError: unsupported operand type(s) for *: 'function' and 'float'

Answer Source

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))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download