问题1
首先,我需要创建一个Person类。然后,我必须在Person类中编写一个状态方法。该方法应调用bmi()方法,并根据返回的结果,使用以下规则将用户的状态作为字符串返回:
BMI Status
< 18.5 Underweight
>= 18.5 and < 25 Normal
>= 25 and < 30 Overweight
>= 30 Obese
我做到了。现在,我要执行以下操作:
问题2
在该类之后添加一个函数read_people(csv_filename),该函数将人员数据的CSV文件的名称作为参数,并返回人员对象的列表,每个对象对应于输入文件中的每一行(顺序相同)。该功能仅需要7-10行,注释和空白行除外。此问题的最大函数长度设置为10条语句。
输入的CSV文件将包含4列:
a name (not containing a comma)
an integer age (in years)
a float weight (in kg)
a float height (in metres)
例如,在下面的示例中使用的文件people1.csv包含以下内容:
Rolly Polly,47,148.8,1.67
Rosie Donell,23,89.4,1.82
Rambo Stallone,19,59,2.0
Sharon Stone,14,50,1.6
Arnold Shwarnegger,70,59.2,1.65
测试用例
persons = read_people("people1.csv")
for person in persons:
print(person)
预期结果
Roly Polly (47) has a bmi of 53.35. Their status is Obese.
Rosie Donell (23) has a bmi of 26.99. Their status is Overweight.
Rambo Stallone (19) has a bmi of 14.75. Their status is Underweight.
Sharon Stone (14) has a bmi of 19.53. Their status is Normal.
Arnold Shwarnegger (70) has a bmi of 21.74. Their status is Normal.
我尝试了以下代码(并且失败)
"""File for creating Person objects"""
class Person:
"""Defines a Person class, suitable for use in a hospital context.
Methods: bmi()
"""
def __init__(self, name, age, weight, height):
"""Creates a new Person object with the specified name, age, weight
and height"""
self.name = name
self.age = age
self.weight = weight
self.height = height
def bmi(self):
"""Returns the body mass index of the person"""
return self.weight / (self.height * self.height)
def status(self):
"""dsjhf dsfkj"""
status = ""
bmi = Person.bmi(self)
if bmi < 18.5:
status = "Underweight"
if bmi >= 18.5 and bmi < 25:
status = "Normal"
if bmi >= 25 and bmi < 30:
status = "Overweight"
if bmi >= 30:
status = "Obese"
return status
def __str__(self):
"""outputs data"""
ans1 = Person.bmi(self)
ans2 = Person.status(self)
answer = "{0} ({1}) has a bmi of {2:.02f}. Their status is {3}."
return answer.format(self.name, self.age, ans1, ans2)
def read_people(csv_filename):
"""reads file and sorts it then runs through a class"""
lst = []
final = []
file = open(csv_filename, 'r')
for row in file:
lst.append(row)
return lst
persons = read_people("people1.csv")
for person in persons:
print(person)
我的输出:
Traceback (most recent call last):
File "C:/Users/Jason/untitled-2.py", line 61, in <module>
for person in persons:
builtins.TypeError: 'NoneType' object is not iterable
问题3
好像那还不够,现在,我需要添加一个新函数filter_people(people,status),该函数接受人员列表和“ status”字符串,并返回一个仅包含原始列表中健康状态等于其状态的人员的新列表状态参数。请注意,该函数是全局函数,而不是Person类的方法。
函数filter_people(people,status)应该只有大约6行代码,不包括注释。与上一个问题一样,最大函数长度设置为10条语句。
测试
persons = read_people("people1.csv")
for status in ['Underweight', 'Normal', 'Overweight', 'Obese']:
persons_with_status = filter_people(persons, status)
print("People who are {}:".format(status))
for person in persons_with_status:
print(person)
print()
预期结果:
People who are Underweight:
Rambo Stallone (19) has a bmi of 14.75. Their status is Underweight.
People who are Normal:
Sharon Stone (14) has a bmi of 19.53. Their status is Normal.
Arnold Shwarnegger (70) has a bmi of 21.74. Their status is Normal.
People who are Overweight:
Rosie Donell (23) has a bmi of 26.99. Their status is Overweight.
People who are Obese:
Roly Polly (47) has a bmi of 53.35. Their status is Obese.
我要去哪里错了。我不断收到以下错误:
builtins.TypeError: 'NoneType' object is not iterable
我需要问题2和问题3的帮助。帮助!!!
最佳答案
def read_people(csv_filename):
"""reads file and sorts it then runs through a class"""
lst = []
final = []
file = open(csv_filename, 'r')
for row in file:
print(row)
lst.append(row)
您应该在函数末尾返回列表