我目前正在处理这里的最后一个功能,timeToRun。
我不知道为什么要收到
NameError: name 'caloriesBurned' is not defined
我正在尝试计算一定重量的人跑步时消耗掉许多卡路里所需的分钟数。
weight = (int(input("Enter your weight in pounds ")))
while weight <= 40:
weight = int(input("Please reenter, weight must be higher than 40. "))
height = (int(input("Enter your height in inches ")))
while height <= 30:
height = int(input("Please reenter, height must be higher than 30. "))
age = (int(input("Enter your age in years ")))
while age <= 1:
age = int(input("Please reenter, age must be higher than 1. "))
def CalorieBurn(user_weight, user_height, user_age):
calories = 655 + (4.3 * user_weight) + (4.7 * user_height) - (4.7 * user_age)
print(calories)
def burnedRuns(user_weight):
caloriesBurned = user_weight * .095
print(caloriesBurned)
def burnedJogs(user_weight):
caloriesBurned = user_weight * .0775
print(caloriesBurned)
def burnedWalks(user_weight):
caloriesBurned = user_weight * .054
print(caloriesBurned)
def timeRequiredRun(caloriesDaily, user_weight):
caloriesBurned = user_weight * .095
timeToRun = calories / caloriesBurned
print(timeToRun)
timeRequiredRun(caloriesBurned, user_weight)
答案 0 :(得分:1)
您的最后一行timeRequiredRun(caloriesBurned, user_weight)
使用一个名为caloriesBurned
的变量,该变量不存在。我认为您正在寻找这样的东西:
def CalorieBurn(user_weight, user_height, user_age):
calories = 655 + (4.3 * user_weight) + (4.7 * user_height) - (4.7 * user_age)
return calories
def timeRequiredRun(calories, user_weight):
caloriesBurned = user_weight * .095
timeToRun = calories / caloriesBurned
print(timeToRun)
calories = CalorieBurn(weight, height, age)
timeRequiredRun(calories, user_weight)
答案 1 :(得分:0)
您已定义函数中消耗的卡路里。这意味着它仅在该本地范围内,您不能在该函数之外调用它。如果需要,可以使用return
关键字在函数末尾返回该变量,例如return caloriesBurned
。如果您随后将变量设置为函数,例如var = func()
并且该函数的末尾有return caloriesBurned
,则它现在等于变量。
似乎您在调用timeRequiredRun(caloriesBurned, user_weight)
时使用了错误的变量。也许您忘了问用户每天消耗多少卡路里?