简体   繁体   中英

Nested If Statement with multiple conditions

My code won't reach the "if" statement piece of my code - does not display error though

name = input("Name: ")
gender = input("Gender: (Male or Female) ").lower()
age = input("Age: ")
weight = input("Weight: ")
height = input("Height: ")
activity_level = input("Activity level on a scale of 1 to 5: ")

if gender == 'male':
    bmr = (66 + (6.3*int(weight)) + (12.9*int(height)) - (6.8*int(age)))
    if activity_level == 1:
        calorie = bmr * 1.2
    elif activity_level == 2:
        calorie = bmr * 1.375
    elif activity_level == 3:
        calorie = bmr * 1.55
    elif activity_level == 4:
        calorie = bmr * 1.725
    elif activity_level == 5:
        calorie = bmr * 1.9
        print(f"Hello {name}, you need to consume {calorie} calories/day to maintain your current weight")
elif gender == "female":
    bmr = (655 + (4.3*int(weight)) + (4.7*int(height)) - (4.7*int(age)))
    if activity_level == 1:
        calorie = bmr * 1.2
    elif activity_level == 2:
        calorie = bmr * 1.375
    elif activity_level == 3:
        calorie = bmr * 1.55
    elif activity_level == 4:
        calorie = bmr * 1.725
    elif activity_level == 5:
        calorie = bmr * 1.9
        print(f"Hello {name}, you need to consume {calorie} calories/day to maintain your current weight")

I expect the code to calculate calories needed based on if the user is a male or female, and their inputted activity level and then print out the statement

input returns a string so: activity_level = input("Activity level on a scale of 1 to 5: ")

activity_level is a string and in the if statements you compare string to ints like this:

if activity_level == 1:

so you need to cast the input to a int like this:

activity_level = int(input("Activity level on a scale of 1 to 5: "))

You are getting calories is not defined because the program never goes into any of the if or elif statements

you can define calories before this statement if gender == 'male': to calorie = 0 that way you don't have to call print(...) under the elifs

You can replace all those if statements via an input list, which will be processes using List Comprehension . I also create separate functions to handle all the sub actions.

def male_calorie_intake (name, age, weight, height, activity_level):
   bmr = (66 + (6.3 * int(weight)) + (12.9 * int(height)) - (6.8 * int(age)))

   # This list contains the activity level (1-5) and their associated multiplier
   activity_levels = [(1, 1.2), (2, 1.375), (3, 1.55), (4,1.725), (5, 1.9)]

   # This list comprehension takes the user's inputted activity_level
   # and compares this level to activity_levels slice x[0] (e.g. 1).
   # The output is the matching activity level and its associated multiplier
   current_activity_level = [(x[0], x[1]) for x in activity_levels if x[0] == int(activity_level)]
   if current_activity_level:

     # This list comprehension slices the multiplier from the
     # list current_activity_level
     bmr_multiplier = [x[1] for x in current_activity_level]

     # The slice containing the multiplier is passed to the
     # match function
     calorie = bmr * bmr_multiplier[0]
     return name, calorie

def female_calorie_intake (name, age, height, weight, activity_level):
  bmr = (655 + (4.3 * int(weight)) + (4.7 * int(height)) - (4.7 * int(age)))

  # This list contains the activity level (1-5) and their associated multiplier
  activity_levels = [(1, 1.2), (2, 1.375), (3, 1.55), (4,1.725), (5,1.9)]

  # This list comprehension takes the user's inputted activity_level
  # and compares this level to activity_levels slice x[0] (e.g. 1).
  # The output is the matching activity level and its associated multiplier
  current_activity_level = [(x[0], x[1]) for x in activity_levels if x[0] == int(activity_level)]

  if current_activity_level:

    # This list comprehension slices the multiplier from the
    # list current_activity_level
    bmr_multiplier = [x[1] for x in current_activity_level]

    # The slice containing the multiplier is passed to the
    # match function
    calorie = bmr * bmr_multiplier[0]
    return name, calorie

def obtain_input():
  name = input("Name: ")
  gender = input("Gender: (Male or Female) ").lower()
  age = input("Age: ")
  weight = input("Weight: ")
  height = input("Height: ")
  activity_level = input("Activity level on a scale of 1 to 5: ")
  return name, gender, age, weight, height, activity_level


input_data = obtain_input()
if input_data[1] == 'male':
  results = male_calorie_intake(input_data[0], input_data[2], input_data[3], input_data[4], input_data[5])
  print(f"Hello {results[0]}, you need to consume {results[1]} calories/day to maintain your current weight")
elif input_data[1] == 'female':
  results = female_calorie_intake(input_data[0], input_data[2], input_data[3], input_data[4], input_data[5])
  print (f"Hello {results[0]}, you need to consume {results[1]} calories/day to maintain your current weight")

It's reaching it, you have have the print statement under the last 'elif' so it's not reaching that part. you need to unindent:

name = input("Name: ")
gender = input("Gender: (Male or Female) ").lower()
age = input("Age: ")
weight = input("Weight: ")
height = input("Height: ")
activity_level = int(input("Activity level on a scale of 1 to 5: "))

if gender == 'male':
    bmr = (66 + (6.3*int(weight)) + (12.9*int(height)) - (6.8*int(age)))
    if activity_level == 1:
        calorie = bmr * 1.2
    elif activity_level == 2:
        calorie = bmr * 1.375
    elif activity_level == 3:
        calorie = bmr * 1.55
    elif activity_level == 4:
        calorie = bmr * 1.725
    elif activity_level == 5:
        calorie = bmr * 1.9
    print(f"Hello {name}, you need to consume {calorie} calories/day to maintain your current weight")
elif gender == "female":
    bmr = (655 + (4.3*int(weight)) + (4.7*int(height)) - (4.7*int(age)))
    if activity_level == 1:
        calorie = bmr * 1.2
    elif activity_level == 2:
        calorie = bmr * 1.375
    elif activity_level == 3:
        calorie = bmr * 1.55
    elif activity_level == 4:
        calorie = bmr * 1.725
    elif activity_level == 5:
        calorie = bmr * 1.9
    print(f"Hello {name}, you need to consume {calorie} calories/day to maintain your current weight")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM