Python Forum
python_Basic_variable_function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python_Basic_variable_function
#1

please explain to me following command lines. we created variable name1,then we def function bmi_calculator in that function we wrote three arguments. in which one is 'name' which is same used in if condition as name + "is not over weight". what i dont understand that how these two name1 and name are linked. because in the result variable we moved to the name1, height_m1.... can someone explain to me the following codes working concept. i cant understand the result "jobs is not overweight" is achieved. thanks

name1 = "jobs"
height_m1 = 2
weight_kg1 = 90

def bmi_calculator(name, height_m, weight_kg):
    bmi = weight_kg / (height_m ** 2)

    if bmi < 25:
        return name + " is not overweight"
    else:
        return name + " is overweight"

result = bmi_calculator(name1, height_m1, weight_kg1)
print(result)
result will come: jobs is not overweight

Reply
#2
You assign values to 3 variables (lines 1-3)

you define bmi_calculator function. It takes 3 arguments. (lines 5-11). These arguments will be replaced with values every time you call the function.
inside function on line 6 you calculate bmi
on lines 8 you check if bmi < 25. If that's True, on line 9 you construct string name + " is not overweight. Note that name is replaced with actual value used when calling the function. and you return the created str.
Same on lines 10-11, for the case when bmi >= 25

on line 13, you call the function and you pass the 3 variables (defined on lines 1-3) as arguments. That's the right-hand side. Then you assign the value returned from the function (i.e. the same string from line 9 or line 11) to result variable

on line 14 you print the result

here you can visualise the execution step by step
https://goo.gl/9vWX1T
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
hi there - many thanks dear Buran for the step by step explanation.

Great - and also thanks for the added link to the visualisation

Regards Apollo Smile
Reply


Forum Jump:

User Panel Messages

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