Python Forum
python_Basic_variable_function - 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: python_Basic_variable_function (/thread-16354.html)



python_Basic_variable_function - nomanahmedna - Feb-24-2019


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




RE: python_Basic_variable_function - buran - Feb-24-2019

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


RE: python_Basic_variable_function - apollo - Feb-03-2021

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