Python类和对象编码错误
问题1 首先,我需要创建一个Person类。然后我必须在Person类中编写一个status方法。该方法应调用bmi()方法,并根据返回的结果,使用以下规则以字符串形式返回人员的状态:Python类和对象编码错误,python,python-3.x,Python,Python 3.x,问题1 首先,我需要创建一个Person类。然后我必须在Person类中编写一个status方法。该方法应调用bmi()方法,并根据返回的结果,使用以下规则以字符串形式返回人员的状态: BMI Status < 18.5 Underweight >= 18.5 and < 25 Normal >= 25 and < 30 Overweight >= 30 Obese 例如,以下
BMI Status
< 18.5 Underweight
>= 18.5 and < 25 Normal
>= 25 and < 30 Overweight
>= 30 Obese
例如,以下示例中使用的文件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
测试用例
预期结果
我尝试了以下代码(但失败)
预期结果:
我哪里做错了。我不断得到以下错误:
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)
您应该在函数末尾返回列表不应在
Person
类中定义read\u people
函数。它是一个单独的函数,从文件数据创建Person
实例列表,然后返回该列表。您的read\u people
不会解析文件数据,也不会从该数据创建Person
,也不会返回列表
这是一个修复过的版本。为了使代码自包含,我从字符串列表中读取文件数据,但很容易将其更改为从实际的CSV文件读取
# A fake file
file = '''\
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
'''.splitlines()
class Person:
"""Defines a Person class, suitable for use in a hospital context.
Methods: bmi(), status()
"""
def __init__(self, name, age, weight, height):
""" Create 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):
""" Return the body mass index of the person """
return self.weight / (self.height * self.height)
def status(self):
""" Determine status """
bmi = self.bmi()
if bmi < 18.5:
status = "Underweight"
elif 18.5 <= bmi < 25:
status = "Normal"
elif 25 <= bmi < 30:
status = "Overweight"
elif bmi >= 30:
status = "Obese"
return status
def __str__(self):
""" Output data """
answer = "{0} ({1}) has a bmi of {2:.02f}. Their status is {3}."
return answer.format(self.name, self.age, self.bmi(), self.status())
def read_people(csv_filename):
"""reads file and sorts it then runs through a class"""
lst = []
#file = open(csv_filename, 'r')
for row in file:
name, age, weight, height = row.split(',')
lst.append(Person(name, int(age), float(weight), float(height)))
#file.close()
return lst
persons = read_people("people1.csv")
for person in persons:
print(person)
与其使用name、age、weight、height=row.split(',')
解析文件数据,不如使用标准的csv
模块,但我怀疑对于此分配,您不允许这样做
您为Person
类编写的代码基本上还可以,但我对它做了一些小的调整
现在看看你是否能解决问题3如果你被卡住了,请问一个新问题(如果你愿意,你可以将它链接到这个问题)。读者是否应该返回一些东西。。。?因为堆栈交换问题似乎不需要,所以每个问题应该包含一个问题。这使它们对未来的读者更有用您能在追加之前添加打印以显示包含行的内容吗?@PM 2Ring是对的您的函数不在适当的位置,请检查他的答案
"""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
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
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)
# A fake file
file = '''\
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
'''.splitlines()
class Person:
"""Defines a Person class, suitable for use in a hospital context.
Methods: bmi(), status()
"""
def __init__(self, name, age, weight, height):
""" Create 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):
""" Return the body mass index of the person """
return self.weight / (self.height * self.height)
def status(self):
""" Determine status """
bmi = self.bmi()
if bmi < 18.5:
status = "Underweight"
elif 18.5 <= bmi < 25:
status = "Normal"
elif 25 <= bmi < 30:
status = "Overweight"
elif bmi >= 30:
status = "Obese"
return status
def __str__(self):
""" Output data """
answer = "{0} ({1}) has a bmi of {2:.02f}. Their status is {3}."
return answer.format(self.name, self.age, self.bmi(), self.status())
def read_people(csv_filename):
"""reads file and sorts it then runs through a class"""
lst = []
#file = open(csv_filename, 'r')
for row in file:
name, age, weight, height = row.split(',')
lst.append(Person(name, int(age), float(weight), float(height)))
#file.close()
return lst
persons = read_people("people1.csv")
for person in persons:
print(person)
Rolly 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.