Jan-09-2021, 11:36 AM
The function you shared (without formatting) is incomplete.
This means you already need to know the base and height information for a triangle you want to work out the area of because you need to supply that to the function. So you need code before you call the function to get that information.
You get data directly from the user using the
For example:
You could instead get your function to do the input. It is often better to separate the functions that do calculations from input and output. In the case of the latter, instead of printing anything, the function could simple return the answer (or the complete output including the answer) to be used elsewhere.
Inside your function you say
def mytriangle (base, height): print ("The base is ", base) print ("The height is ", height) base (10) height (15) myarea = base * height / 2Firstly, to use a function you have defined you have to call it. For example:
mytriangle(triangle1_base, triangle1_height)Notice the variables used when calling the function do not need to be the same as the variable names used in the function definition. The latter are names internal to the function, that refer to the same objects referenced when the function is called (either explicitly, by providing literal numbers, or by using variables/expressions).
This means you already need to know the base and height information for a triangle you want to work out the area of because you need to supply that to the function. So you need code before you call the function to get that information.
You get data directly from the user using the
input
function which ALWAYS returns a reference to a str
object, even if the the user only enters digits. In order to do maths on input
information, you need to convert (cast) it to a number, usually either an integer (using int
) or a floating point number (using float
).For example:
triangle1_base = float(input('Base size of first triangle? '))I shall leave you to do the other line.
You could instead get your function to do the input. It is often better to separate the functions that do calculations from input and output. In the case of the latter, instead of printing anything, the function could simple return the answer (or the complete output including the answer) to be used elsewhere.
Inside your function you say
base(10)
but this is telling Python to call a function called base
and pass a reference to the integer object 10
to it.