'Why is my function not printing the try-except-else result?

What is wrong with my code? I am not being able to retrieve data about the age range. I am working with a CSV file but having problems with the try-except-else suite. I am trying to find the average data for a given age range but am not being able to as nothing is printing.

def calculator(min_age, max_age):

    file = open(r"C:\Users\alisha\OneDrive\Desktop\programming\data.csv")

    data = file.read()

    file.close()

    #data processing
    lines = data.split("\n")
    del lines [-1]

    #18 lines
    line1 = lines[1].split(",")
    line2 = lines[2].split(",")
    line3 = lines[3].split(",")
    line4 = lines[4].split(",")
    line5 = lines[5].split(",")
    line6 = lines[6].split(",")
    line7 = lines[7].split(",")
    line8 = lines[8].split(",")
    line9 = lines[9].split(",")
    line10 = lines[10].split(",")
    line11 = lines[11].split(",")
    line12 = lines[12].split(",")
    line13 = lines[13].split(",")
    line14 = lines[14].split(",")
    line15 = lines[15].split(",")
    line16 = lines[16].split(",")
    line17 = lines[17].split(",")
    line18 = lines[18].split(",")

    processed = [line1, line2, line3, line4, line5, line6, line7, line8, line9, line10, line11, line12, line13, line14, line15, line16, line17, line18]

    #obtain height and weight columns
    age = []
    for line in processed:
        age.append(line[2])

    min_value = min(age)

    max_value = max(age)

    target_weight = []
    target_height = []

    for line in processed:
        if int(line[2]) >= min_age and int(line[2]) <= max_age:
            target_weight.append(line[4])
            target_height.append(line[3])
    try:
        calc = ((target_weight/len(target_weight)) / (target_height / len(target_height))**2 )*703

    except:
        if int(min_age) < int(min_value):
            print("Minimum age value too small. Try Again.")
        elif int(max_age) > int(max_value):
            print("Maximum age value too large. Try Again.")
            
    else:
        print(f"The average for the given age range {max_age} to {min_age} is {calc}.")


Solution 1:[1]

sounds like your teacher wants you to create custom error classes to throw, try this:

class ValueTooHighError(Error):
    pass
class ValueTooLowError(Error):
    pass


file_obj = open(r"C:\Users\ahana\OneDrive\Desktop\intro toprogramming\assignment_8\patient_data.csv")

data = file_obj.read()

file_obj.close()
#----left out some code for conciseness----#
min_value = min(age)
max_value = max(age)

target_weight = []
target_height = []

for line in processed:
    try:
        if int(line[2]) >= min_age and int(line[2]) <= max_age:
            target_weight.append(line[4])
            target_height.append(line[3])
        else:
            if(int(line[2])<min_age):
                raise ValueTooLowError
            else:
                raise ValueTooHighError
    except:
        ValueTooLowError:
            print("Minimum age value too small. Try Again.")
        ValueTooHighError:
            print("Maximum age value too large. Try Again.")

bmi = ((sum(target_weight)/len(target_weight)) / (sum(target_height) / len(target_height))**2 )*703
print(f"The average BMI for the given age range {max_age} to {min_age} is {bmi}.")

Solution 2:[2]

There is a lot going on here, first I will address your actual question with the try-except block. I believe you want the except code to run when age value is out of range, but you need to actually generate an exception for the try statement to catch:

def calcBMI(minimum, maximum, val):
  if val<minimum or val>maximum:
    raise ValueError
  # do stuff

try:
  bmi = calcBMI(min_age, max_age, value)
except ValueError:
  print("Error")
else:
  print(bmi)

There is also a lot of bad form going on, you are using 18 separate near-identical lines to do something that only takes one! Those 18 lines where you fill up the processed list can be written as:

processed = [line.split(",") for line in lines]

Another thing is you run the same for-loop 3 times to populate age, height and weight lists. You could do this in one like this:

for line in processed:
  age.append(line[2])
  height.append(line[3])
  weight.append(line[4])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dbakr
Solution 2 Stanley