【问题标题】:I do not know what to put as a parameter in order to assign value to a variable我不知道应该把什么作为参数来为变量赋值
【发布时间】:2025-12-27 19:20:08
【问题描述】:

我现在将它们设置为全局,但我不知道如何设置参数和参数以保存变量 bmr 以便在另一个函数中使用它,例如 get_calorie_requirement。我知道我有很多投入才能获得 bmr。我应该定义公式 get_bmr 吗?我被困在一些简单的事情上,因为我正在尝试做 bmr= get_bmr(bmr) 但这是错误的。

import math
def get_bmr():    
    gender = input("What is your gender: M or F?")
    age = int(input("What is your age?"))
    height = int(input("What is your height in inches?"))
    weight = (int(input("What is your weight in pounds?")))
    bmr_wght_constant = 4.536
    bmr_hght_constant = 15.88
    bmr_age_constant = 5
    if gender == 'M':
        bmr = int((bmr_wght_constant * weight) + (bmr_hght_constant * height) - (bmr_age_constant * age) + 5)
    elif gender == 'F':
        bmr = int((bmr_wght_constant * weight) + (bmr_hght_constant * height) - (bmr_age_constant * age) - 161)
    else:
        print("Please try again.")
    return bmr

def get_daily_calorie_requirement():
    dcr_1 = 1.2
    dcr_2 = 1.375
    dcr_3 = 1.55
    dcr_4 = 1.725
    dcr_5 = 1.9
    act_lvl = int(input("What is your activity level?"))
    if act_lvl == 1:
        daily_calorie_requirement = int(bmr * dcr_1)
    elif act_lvl == 2:
        daily_calorie_requirement = int(bmr * dcr_2)
    elif act_lvl == 3:
        daily_calorie_requirement = int(bmr * dcr_3)
    elif act_lvl == 4:
        daily_calorie_requirement = int(bmr * dcr_4)
    elif act_lvl == 5:
        daily_calorie_requirement = int(bmr * dcr_5)
    else:
        print("Please choose a number 1-5.")
    return daily_calorie_requirement
def main():
    print("Hello, welcome to my intergration project!")
    print("The purpose of this program is to help the user reach their goal and provide helpful suggestions.")
    print("It will do this by taking your age, gender, height and your level of physical activity in order to calculate your Basal Metabolic Rate(BMR)")
    print("Your BMR is how many calories you burn in a single day. Combining your BMR with your goals we can suggest a meal plan and excercises that will help reach your goals")
    print("Let's get started! I will start by asking you a few questions in order to make a profile and give you the best informed advice.")      
    if gender == 'M':
        bmr = int((bmr_wght_constant * weight) + (bmr_hght_constant * height) - (bmr_age_constant * age) + 5)
    elif gender == 'F':
        bmr = int((bmr_wght_constant * weight) + (bmr_hght_constant * height) - (bmr_age_constant * age) - 161)
    else:
        print("Please try again.")
    print("Your BMR is: ", bmr)

    print("Great! Now that we have calculated your Basal Metabolic Rate, let's calculate your daily calorie requirement!")
    print("This is the calories you should be taking in to maintain your current weight")
    print("How active are you on a scale of 1-5?")
    print("1 being  you are sedentary (little to no exercise)")
    print("2 being lightly active (light exercise or sports 1-3 days a week)")
    print("3 being moderately active (moderate exercise 3-5 days a week)")
    print("4 being very active (hard exercise 6-7 days a week)")
    print("5 being super active (very hard exercise and a physical job)")
    print("Exercise would be 15 to 30 minutes of having an elevated heart rate.")
    print("Hard exercise would be 2 plus hours of elevated heart rate.")
    daily_calorie_requirement = get_daily_calorie_requirement()

【问题讨论】:

    标签: python variables functional-programming global-variables


    【解决方案1】:

    向我们展示您所面临的错误会更有帮助,由于代码也无法正常工作,因此很难判断问题可能是什么。如果您有追溯,这将有助于解决此问题。

    = 的左侧有一个变量时,只会发生分配。当调用诸如get_bmr() 之类的方法时,将分配此函数所分配的return

    您的代码可能会失败,因为您尝试获取 gender,但这未在函数 main 下定义。您可以将if gender "M" elif "F" 替换为以下内容。 (既然你在get_bmr中调用了这一切)

    bmr = get_bmr()
    

    如果您想将bmr 传递给下一个函数,是否可以为get_daily_calorie_requirement 创建一个参数来解决问题?

    def get_daily_calorie_requirement(bmr):
        dcr_5 = 1.9
        return int(bmr * dcr_5)
    
    
    def main():
        bmr = get_bmr()
    
        daily_calorie_requirement = get_daily_calorie_requirement(bmr)
    
    
    if __name__ == '__main__':
        main()
    

    如果你想简化它,你可以将一些参数移动到参数中:

    def get_bmr(gender, age, height, weight):
        """ Calculate BMR using the following formula:
    
        bmr = weight_const*weight ... etc.
        """
        gender_coef = 5 if gender == "M" else -161
    
        return int((4.536 * weight) + (15.88 * height) - (5 * age) + 5)
    
    
    gender = input("What is your gender: M or F?")
    while gender not in ["M", "F"]:
        print("Please try again, accepted values: M/F")
        gender = input("What is your gender: M or F?")
    
    age = int(input("What is your age?"))
    height = int(input("What is your height in inches?"))
    weight = int(input("What is your weight in pounds?"))
    
    bmr = get_bmr(gender, age, height, weight)
    

    有些事情可能很棘手,将变量命名为与您的函数相同,或者只是在函数中忘记return

    【讨论】:

    • 非常感谢,我的问题是试图将 bmr 传递到下一个参数中,这是我被卡住的地方。我把它作为 daily_calorie_requirement = get_daily_calorie_requirement(bmr) 的参数,它起作用了。抱歉,我有点含糊,没有发布错误,但您能够回答我的问题。
    • @Mhurtad14 如果它有助于解决您的问题,请不要忘记接受答案^_^
    最近更新 更多