Python Forum
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function exercise
#1
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!!
Reply
#2
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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.
Reply
#4
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?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020