具有多个条件的嵌套If语句

时间:2019-08-05 18:40:04

标签: python python-3.x if-statement

我的代码不会到达我代码的“ if”语句部分-不会显示错误

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")

我希望代码可以根据用户是男性还是女性以及他们输入的活动水平来计算所需的卡路里,然后打印出声明

3 个答案:

答案 0 :(得分:2)

input返回一个字符串,因此: activity_level = input("Activity level on a scale of 1 to 5: ")

activity_level是一个字符串,在if语句中,您将字符串与类似以下内容的int进行比较:

if activity_level == 1:

所以您需要像这样将输入转换为整数:

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

由于程序永远不会进入任何if或elif语句

,因此您没有获取卡路里的定义

您可以在此语句if gender == 'male':calorie = 0之前定义卡路里,这样就不必在省略号下调用print(...)

答案 1 :(得分:2)

您可以通过输入列表替换所有这些 if语句,这些输入列表将使用 List Comprehension 进行处理。我还创建了单独的函数来处理所有子操作。

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")

答案 2 :(得分:1)

达到它,您在最后一个“ elif”下有打印语句,因此它没有达到该部分。您需要取消缩进:

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")