提问人:tranese 提问时间:7/24/2023 最后编辑:tranese 更新时间:7/24/2023 访问量:631
(蟒蛇)为类 PetData 定义成员方法 print_all()。使用基类的 print_all() 方法
(PYTHON) Define a member method print_all() for class PetData. Make use of the base class' print_all() method
问:
任务:为类 PetData 定义成员方法 print_all()。使用基类的 print_all() 方法。 带有输入的给定程序的示例输出:“Fluffy”5 4444 常用中文名:毛茸茸 年龄:5岁 编号: 4444
第一个错误是 ID: 4444 后面没有换行符。当我解决这个问题时,我开始在 ID: 之前得到 NONE 这个词。如果我知道它来自哪里,我可能会弄清楚,但我什么都没有。请帮忙!!
class AnimalData:
def __init__(self):
self.full_name = ''
self.age_years = 0
def set_name(self, given_name):
self.full_name = given_name
def set_age(self, num_years):
self.age_years = num_years
# Other parts omitted
def print_all(self):
print('Name:', self.full_name)
print('Age:', self.age_years)
class PetData(AnimalData):
def __init__(self):
AnimalData.__init__(self)
self.id_num = 0
def set_id(self, pet_id):
self.id_num = pet_id
# FIXME: Add print_all() member method
""" Your solution goes here"""
user_pet = PetData()
user_pet.set_name(input())
user_pet.set_age(int(input()))
user_pet.set_id(int(input()))
user_pet.print_all()```
I've been working on this assignment for 2 days and I'm getting errors no matter what I try. At one point the word NONE was randomly appearing before ID: 4444, still can't figure out where it's coming from. PLEASE HELP!!!!
Here is some of what I've tried and the errors I got.
##
No newline after ID: 4444
def print_all(self):
AnimalData.print_all(self)
print('ID:', self.pet_id)
##
ID number not showing in output.
def print_all(self):
print(AnimalData.print_all(self), 'ID:', self.id_num)
##
No newline after ID: 4444
user_pet = PetData()
user_pet.set_name("Fluffy")
user_pet.set_age(5)
user_pet.set_id(4444)
user_pet.print_all()
答:
0赞
tranese
7/24/2023
#1
正确答案是:
def print_all(self):
AnimalData.print_all(self)
print('ID:', self.id_num)
感谢您@chepner的帮助!!
评论
print