functions and indentation in python

前端 未结 3 1603
广开言路
广开言路 2021-01-29 14:32

I\'m taking a tutorial in udemy to teach myself Python and it is making me love Javascript every day more. Anyhow, I can\'t seem to figure out how to work this indentation right

3条回答
  •  無奈伤痛
    2021-01-29 15:26

    You are getting the weight, height and bmi, but you are just throwing away the result. These variables are local to the inner function scopes, so they are invisible for your outer function.

    Doing:

    weigth = getWeight()
    height = getHeight()
    bmi    = calcBMI(weight, height)
    

    Fixes the problem.

    Also, a note on squiggly brackets and semicolons; In Python an indented block with the same indentation level is equivalent to a pair of squiggly brackets in other languages. While line breaks translates to semicolons.

        a = 1
        b = 2
        if True:
            c = 3
    

    <=>

    {
        a = 1;
        b = 2;
        if (True) {
            c = 3;
        }
    }
    

    However, there are a few (convenient) exceptions. Like when a line is ended without a closing parenthesis, no "virtual semicolon" is inserted. Also, an inner Python scope does not hide its local variables to an outer scope unless the scoping is due to a function.

提交回复
热议问题