【发布时间】: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