Python Forum
function exercise - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: function exercise (/thread-6337.html)



function exercise - tdebeaulieu - Nov-16-2017

Hi there, I just took a python course and I´m just lost..
Can you please give me a hand with the following?


IMC = mass / (height^ 2)
Where mass is expressed in kilograms and height in meters.
To solve the problem you must write the missing code.
Asume both mass and height variables already exist, so there´s no need to ask for them

def imc(mass, height):
imc = 0
# from here you must modify the program
# modify the variable imc
# remember the data for the variables mass and height exist
return imc


Any help is wellcome!!


RE: function exercise - sparkz_alot - Nov-17-2017

The answer is in your post. Remember, proper indentation is very important in Python. Also, "imc" is the name of your function, so you don't want to use it as a variable.  Take another look at the pseudo code and then give it a try


RE: function exercise - iFunKtion - Nov-17-2017

sparkz_alot is correct, I would like to just add, that the question is slightly tricking you by using "imc" for the function name and a variable name.

basically, the first line is correct, the function is called imc, and two variable are being passed to the function, mass and height. These would come from elsewhere in a program and likely passed to the function something like:
something = imc(5, 4)
where
mass = 5 
height = 4
in my example here. So you have the values already given you and you use the variables of "mass" and "height" within your function.

You are then asked to modify the variable "imc" so you need to use a different varible, "answer", "x", "doris" anything will do so long as it obeys the python syntax and is not a keyword, but convention is to use something meaningful and descriptive (better to have a longer variable name and know what it is than just use "x" and not understand it in a months time).

Then you will be returning that variable from the function, so what ever variable you chose, will be the return values:
return answer
where "answer" is the variable I have just used.

You can always just write the function into a python prompt (don't forget the 4 space indentation on each line of the function) then once it is written, enter twice will have the function defined in memory, then simply go:
imc(10, 12)
or whatever values you want to test it.

I hope that clears it up a little, I found passing variables to and from functions a little tricky when I was first learning this.


RE: function exercise - tdebeaulieu - Nov-20-2017

Guys, thanks for the replies, however, I´m not following you. I just started this course last week and have no background at all in coding.
How do you modify imc?