2016-10-24 4 views
0
def BMI_calculator(inches, weight): 
    """ This function takes a persons height in feet and inches and their 
weight in pounds and calculates their BMI""" 

# Now we can just convert and calculate BMI 

    metric_height= inches* .025 
    metric_weight= weight* .45 
    BMI = int(metric_weight/(metric_height)**2) 
    print BMI 
    return BMI 

def BMI_user_calculator(): 
    """ This function will ask the user their body information and calulate 
the BMI with that info""" 
# First we need to gather information from the user to put into the BMI calculator 
    user_weight = int(raw_input('Enter your weight in pounds: ')) 
    user_height = int(raw_input('Enter your height in inches: ')) 

    print "Youre BMI is", 

    BMI_calculator(user_height, user_weight) 

    If BMI < 18.5: 
     print "You're BMI is below the healthy range for your age" 
    elif BMI > 24.9: 
     print "You're BMI is above the healthy range for your age" 
    else: 
     print "You are in the healthy BMI range!" 

이 말했다 내가 지금까지 가지고있는 코드되고있는 이유 이해하지 않지만, 실행 내가 BMI 말을 내 if 문 내에서 구문 오류가 발생하면 정의되지 않았습니다. BMI이 첫 번째 함수에서 반환되었으므로 실제로 어떤 일이 발생하는지 이해하지 못합니다.내가 내가 내가 여기에 구문 오류

+0

당신의'BMI' 변수는 BMI_Calculator''내에 존재 :

코드는 다음과 같이 뭔가를 읽어야한다. user_calculator에서 리턴 값을 포착하지 않으므로 거기에있는'BMI'는 정의되지 않은 변수입니다. 그리고 언어학적인 측면에서, "너"-> "너는". "당신은 bmi입니다"는 말이되지 않습니다. –

답변

1

BMI_user_calculator()BMI이 없으므로 BMI가 정의되어 있지 않습니다. if-elif 문에서 비교하기 전에 먼저 BMI를 선언해야합니다.

또한 Ifif이어야합니다. 파이썬은 대소 문자를 구분합니다.

def BMI_calculator(inches, weight): 
    metric_height= inches* .025 
    metric_weight= weight* .45 
    BMI = int(metric_weight/(metric_height)**2) 
    # No need to print BMI here anymore since you're returning it anyway 
    # print BMI 
    return BMI 

def BMI_user_calculator(): 
    user_weight = int(raw_input('Enter your weight in pounds: ')) 
    user_height = int(raw_input('Enter your height in inches: ')) 

    BMI = BMI_calculator(user_height, user_weight) 

    print "Your BMI is", BMI 

    if BMI < 18.5: 
     print "Your BMI is below the healthy range for your age" 
    elif BMI > 24.9: 
     print "Your BMI is above the healthy range for your age" 
    else: 
     print "You are in the healthy BMI range!" 

if __name__ == '__main__': 
    BMI_user_calculator() 
+0

정말 굉장한데 더 감각적입니다. 감사합니다 !! – jchud13

+0

당신을 진심으로 환영합니다! –

+0

@ jchud13 정답으로 표시하면 도움이 될 것입니다. :) –

관련 문제