functions and indentation in python

前端 未结 3 1601
广开言路
广开言路 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:38

    You are accessing a variable outside the scope. For instance the weight variable is defined in the scope of the getWeight() function but you are trying to access it from the BMICalculator() scope.

    Quick solution

    You should properly scope your variables, you have two options. Define the functions as closures or just use the return value. Below I how the second option.

    def BMICalculator():
        student_name = input("Enter the student's name or 0 to exit:")
        while student_name != "0":
            def getWeight():
                weight = float(input("Enter " + student_name + "'s weight in pounds: "))
                return weight
    
            def getHeight():
                height = float(input("Enter " + student_name + "'s height in pounds: "))
                return height
    
            def calcBMI(weight, height):
                bmi = (weight * 703) / (height * height)
                return bmi
    
            # here you have to define the weight, height and bmi
            # variables in this scope.
            weight = getWeight()
            height = getHeight()
            bmi = calcBMI(weight, height)
            print("\n" + student_name + "'s BMI profile:")
            print("---------------")
            print("Height:", height, "\nWeight:", weight)
    
            def showBMI(bmi):
                print("BMI Index:", "{:.2f}".format(bmi))
    
            showBMI(bmi)
    
            student_name = input("Enter next student's name or 0 to exit:")
    
    
        print("\n\nExiting Program!")
    

    Comments

    To clarify, you should try moving the functions outside the loop (putting them at the same level as BMICalculator(). This will probably clarify the scoping problem to you.

    def getWeight():
        weight = float(input("Enter " + student_name + 
                             "'s weight in pounds: "))
        return weight
    
    def getHeight():
        height = float(input("Enter " + student_name + 
                             "'s height in pounds: "))
        return height
    
    def calcBMI(weight, height):
        bmi = (weight * 703) / (height * height)
        return bmi
    
    def BMICalculator():
        student_name = input("Enter the student's name or 0 to exit:")
        while student_name != "0":
            # Now it is more evident that the weight, height and bmi
            # variables in the above functions are only defined in the
            # scope of the respective function, the BMICalculator scope
            # is different and the names "weight", "height" and "bmi" are
            # not bound to anything, hence the NameError.
            weight = getWeight()
            height = getHeight()
            bmi = calcBMI(weight, height)
            print("\n" + student_name + "'s BMI profile:")
            print("---------------")
            print("Height:", height, "\nWeight:", weight)
    
            def showBMI(bmi):
                print("BMI Index:", "{:.2f}".format(bmi))
    
            showBMI(bmi)
    
            student_name = input("Enter next student's name or 0 to exit:")
    

    Scoping in python has some simple yet important rules, I suggest you have a look at this answer for a quick overview of the scope resolution.

    As an aside, the problem you are having is not python-related, any decent (or at least those I know) programming language (even javascript) would treat those variables as out of scope.

    Hope this helps.

提交回复
热议问题