functions and indentation in python

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

    didn't I define them already in their functions

    Yes, and that's the problem. They are locally scoped to those functions. You need assignment... weight = getWeight(), for example


    No need to go overboard with the function definitions, though

    def calcBMI(weight, height):
        bmi = (weight * 703) / (height * height)
        return bmi
    
    def showBMI(bmi):
        print("BMI Index:", "{:.2f}".format(bmi))
    
    student_name = input("Enter the student's name or 0 to exit:")
    while student_name != "0":
        weight = float(input("Enter " + student_name + "'s weight in pounds: "))
        height = float(input("Enter " + student_name + "'s height in pounds: "))
    
        bmi = calcBMI(weight, height)
    
        # etc...
    
        showBMI(bmi)
        student_name = input("Enter next student's name or 0 to exit:")
    
    print("\n\nExiting Program!")
    

提交回复
热议问题