[英]How do I make this python run program?
class Pet(object):
"""
Object that contains attributes relating to pets
Methods:
__init__: initializes a new object
__str__: prints an object
is_heavier: compares two pets' weights. Return True if self is heavier
than second, otherwise returns False.
is_older: compares two pets' ages. Returns true if self is older
than second, otherwise returns False.
Same_colour: compares two pets' colours. Returns True if colours match
Returns False if Colours don't match.
Attributes:
species: str of type of pet IE "dog" or "giraffe"
name: str listing the name of your pet IE "Joy" (She's a dog")
weight: float the weight of the pet in killograms
height: float the height of the pet in centimetres
age: int the age of the pet in years.
"""
def __init__(self, name, animal_type, age, weight, height):
self.__name = name
self.__animal_type = animal_type
self.__age = age
self.__heavier = weight
self.__taller = height
def set_name(self, name):
self.__name = name
def set_type(self, animal_type):
self.__animal_type = animal_type
def set_age(self, age):
self.__age = age
def get_name(self):
return self.__name
def get_animal_type(self):
return self.__animal_type
def get__age(self):
return self.__age
def get__heavier(self,heavier):
return self.__weight
def get__taller(self, taller):
return self.__height
def main():
name = input('What is the name of the pet: ')
animal_type = input('What type of pet is it: ')
age = int(input('How old is your pet: '))
pets = Pet(name, animal_type, age)
heavier = int(input('How much does your pet weight?: ')
print('This will be added to the records.')
print('Here is the data you entered: ')
print('Pet Name: ', pets.get_name())
print('Animal Type: ', pets.get_animal_type())
print('Age: ', pets.get__age())
print('Kg: ', pets.get__heavier())
main()
所以这应该在上周四之前完成,我仍在做这个作业,因为它被隔离了,所以我的老师不能真正帮助我完成这项工作,我有点想通了,但它一直给我一个诸如“打印”之类的代码错误位置的错误是错误的或类似的东西。 我认为这段代码有问题,我无法弄清楚这段代码为什么或有什么问题。 请各位大神帮我解释一下好吗?
标题不允许我自己制作,所以我必须选择那个。 不是我的错: :)
get__heavier
和get__taller
使用了不存在的变量下面是工作拷贝程序
class Pet(object):
"""
Object that contains attributes relating to pets
Methods:
__init__: initializes a new object
__str__: prints an object
is_heavier: compares two pets' weights. Return True if self is heavier
than second, otherwise returns False.
is_older: compares two pets' ages. Returns true if self is older
than second, otherwise returns False.
Same_colour: compares two pets' colours. Returns True if colours match
Returns False if Colours don't match.
Attributes:
species: str of type of pet IE "dog" or "giraffe"
name: str listing the name of your pet IE "Joy" (She's a dog")
weight: float the weight of the pet in killograms
height: float the height of the pet in centimetres
age: int the age of the pet in years.
"""
def __init__(self, name, animal_type, age, weight, height):
self.__name = name
self.__animal_type = animal_type
self.__age = age
self.__heavier = weight
self.__taller = height
def set_name(self, name):
self.__name = name
def set_type(self, animal_type):
self.__animal_type = animal_type
def set_age(self, age):
self.__age = age
def get_name(self):
return self.__name
def get_animal_type(self):
return self.__animal_type
def get__age(self):
return self.__age
def get__heavier(self):
return self.__heavier
def get__taller(self):
return self.__taller
def main():
name = input('What is the name of the pet: ')
animal_type = input('What type of pet is it: ')
age = int(input('How old is your pet: '))
weight = int(input('How much does your pet weight?: '))
height = int(input('How much does your pet height?: '))
pets = Pet(name, animal_type, age, weight, height)
print('This will be added to the records.')
print('Here is the data you entered: ')
print('Pet Name: ', pets.get_name())
print('Animal Type: ', pets.get_animal_type())
print('Age: ', pets.get__age())
print('Kg: ', pets.get__heavier())
main()
我认为错误是您基于否或论点产生的。 如果你观察你的代码
def __init__(self, name, animal_type, age, weight, height):
self.__name = name
self.__animal_type = animal_type
self.__age = age
self.__heavier = weight
self.__taller = height
总共需要 5 个参数,而您只传递了 3 个参数
pets = Pet(name, animal_type, age)
正确的格式
pets = Pet(name, animal_type, age,weight,height)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.