为什么我的函数不打印 try-except-else 结果?
python 321
原文标题 :Why is my function not printing the try-except-else result?
我有一个家庭作业问题:
“编写一个程序,要求用户输入最小和最大年龄。然后调用函数 BMI_calculator() 来计算和打印给定年龄范围的平均 BMI。你的脚本应该包含一个 try-except-else 机制,它将(1) 如果您输入的年龄小于或大于患者列表中的年龄,则打印单独的错误消息,并且 (2) 提示用户重新输入数据,直到给出适当的年龄范围”
我的代码有什么问题?我无法检索有关年龄范围 BMI 的数据。
CSV 文件:我在函数中使用的 CSV 文件
def BMI_calculator(min_age, max_age):
file_obj = open(r"C:\Users\ahana\OneDrive\Desktop\intro toprogramming\assignment_8\patient_data.csv")
data = file_obj.read()
file_obj.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])
height = []
for line in processed:
height.append(line[3])
weight = []
for line in processed:
weight.append(line[4])
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:
bmi = ((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 BMI for the given age range {max_age} to {min_age} is {bmi}.")
回复
我来回复-
dbakr 评论
听起来你的老师希望你创建自定义错误类来抛出,试试这个:
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}.")
2年前 -
Stanley 评论
这里发生了很多事情,首先我将使用 try-except 块解决您的实际问题。我相信您希望
except
代码在年龄值超出范围时运行,但您需要实际生成异常以使try
语句捕获: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)
还有很多不好的形式,你正在使用 18 条几乎相同的独立行来做一些只需要一条的事情!填写
processed
list的那18行可以写成:processed = [line.split(",") for line in lines]
另一件事是您运行相同的 for 循环 3 次来填充年龄、身高和体重列表。你可以这样做:
for line in processed: age.append(line[2]) height.append(line[3]) weight.append(line[4])
2年前