从函数python调用函数

从函数python调用函数,python,function,Python,Function,我正在做一个燃烧卡路里的简单计算。 我从用户那里得到数据和变量,我有两个公式 BMR功能起作用。发球台不断地抛出错误,主要是在不能调用浮球的情况下,如果没有,我会得到类似的结果 当你这样做的时候 def tee(bmr, amount_of_exersize): 您正在重新定义名称bmr,以指向您传入的任何变量-然而,如果您没有这样做,它将引用上面的函数。只要您在方法中,此替换就适用,唯一真正的解决方案是重命名参数,使其不冲突: def tee(something_else, amount_o

我正在做一个燃烧卡路里的简单计算。 我从用户那里得到数据和变量,我有两个公式

BMR功能起作用。发球台不断地抛出错误,主要是在不能调用浮球的情况下,如果没有,我会得到类似的结果

当你这样做的时候

def tee(bmr, amount_of_exersize):
您正在重新定义名称bmr,以指向您传入的任何变量-然而,如果您没有这样做,它将引用上面的函数。只要您在方法中,此替换就适用,唯一真正的解决方案是重命名参数,使其不冲突:

def tee(something_else, amount_of_exersize):
    y = bmr(gender, age, height, weight)

有几个问题,您正在重新定义bmr,并且没有将正确的参数传递给第二个bmr调用。试试看,例如:

定义性别、年龄、身高、体重、运动量: y=B身高、年龄、身高、体重 如果执行量=0: x=1.2
elif 1你怎么称呼tee?请清楚地说明错误以及它在函数中的位置,我看不出您在调用bmrgender、age、height时定义了性别、年龄、身高、体重,体重在T台上bmr在T台上做什么?bmr女性=655+体重*9.6+身高*1.8+年龄*4.7\\bmr男性=66+体重*13.8+身高*5+年龄*6.8\\如果每周锻炼天数=0,那么tee=bmr*1.2\\如果每周锻炼天数=1-2,那么tee=bmr*1.375\\如果每周锻炼天数=3-5,那么tee=bmr*1.55\\如果每周锻炼天数为6-7天,则TEE=BMR*1.725请不要在注释中添加代码,而应在问题中添加代码。问题中的代码可以正确格式化,注释中的代码很难阅读。文本都错了。没有标点符号,没有遗漏或不完整的单词,…第一个非常好用。第二个更优雅,但仍然会给我错误。对不起,我没有更改bmr-y值,它现在应该可以工作了
def tee(something_else, amount_of_exersize):
    y = bmr(gender, age, height, weight)
def validate_user_input(prompt, type_=None, min_=None, max_=None, range_=None):
    if min_ is not None and max_ is not None and max_ < min_:
        raise ValueError("min_ must be less than or equal to max_.")
    while True:
        ui = input(prompt)
        if type_ is not None:
            try:
                ui = type_(ui)
            except ValueError:
                print("Input type must be {0}.".format(type_.__name__))
                continue
        if max_ is not None and ui > max_:
            print("Input must be less than or equal to {0}.".format(max_))
        elif min_ is not None and ui < min_:
            print("Input must be greater than or equal to {0}.".format(min_))
        elif range_ is not None and ui not in range_:
            if isinstance(range_, range):
                template = "Input must be between {0.start} and {0.stop}."
                print(template.format(range_))
            else:
                template = "Input must be {0}."
                if len(range_) == 1:
                    print(template.format(*range_))
                else:
                    print(template.format(" or ".join((", ".join(map(str,
                                                                     range_[:-1])),
                                                       str(range_[-1])))))
        else:
            return ui


def bmr(gender, age, height, weight):
    if gender == "f":
        bmr = 655 + weight*9.6 + height*1.6 + age*4.7
    else:
        bmr = 66 + weight*13.8 + height*5 + age*6.8
    return bmr

def tee (gender, age, height, weight, amount_of_exersice):
    y = bmr(gender, age, height, weight)
    if amount_of_exersice == 0:
        x = 1.2
    elif 1<= amount_of_exersice <= 2:
        x = 1.375
    elif 3 <= amount_of_exersice <= 5:
        x = 1.55
    else:
        x = 1.725
    z = x * y
    return z

## user info ##
gender = validate_user_input("Please enter your gender (F -Female / M- Male): ", str.lower, range_=("f","m"))
age = validate_user_input("Please enter your age: ", int, 1, 110)
height = validate_user_input("Please enter your height in cm: ", int, 130, 230)
weight = validate_user_input("Please enter your weight in kg: ", float, 20, 400)
amount_of_exersice = validate_user_input("Please enter the amount of days you engage in physical activity per week: ", int, 0, 7)

calc_bmr = bmr(gender, age, height, weight)
calc_tee = tee(gender, age, height, weight, amount_of_exersice)
print("Your Basal Metabolic Rate is ", calc_bmr)
print("Your daily calories burn is ", calc_tee)