简体   繁体   English

如何让这个 python 运行程序?

[英]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()

So This is supposed to be done by last Thursday, and I am STILL working on this assignment, and since it is quarantined so my teacher can't really help me with this work, and I kinda figure it out but it keeps giving me an error of the wrong spot of the code like the "print" is wrong or something like that.所以这应该在上周四之前完成,我仍在做这个作业,因为它被隔离了,所以我的老师不能真正帮助我完成这项工作,我有点想通了,但它一直给我一个诸如“打印”之类的代码错误位置的错误是错误的或类似的东西。 I think something is wrong with this code, and I can't figure out why or what is wrong with this code.我认为这段代码有问题,我无法弄清楚这段代码为什么或有什么问题。 Can you guys please please please help me with good explanations?请各位大神帮我解释一下好吗?

the Title won't let me make my own so I HAD to choose that one.标题不允许我自己制作,所以我必须选择那个。 NOT MY FAULT: :)不是我的错: :)

  1. you forgot to pass all argument to the Pet class when you initiated it您在启动时忘记将所有参数传递给 Pet class
  2. get__heavier and get__taller used not existed variables get__heavierget__taller使用了不存在的变量

Below is the working copy 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):
        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()

I think error your generated on the basis of No or argument.我认为错误是您基于否或论点产生的。 if you observe your code如果你观察你的代码

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

require total 5 argument while you passed only 3 of them总共需要 5 个参数,而您只传递了 3 个参数

pets = Pet(name, animal_type, age)

correct format正确的格式

pets = Pet(name, animal_type, age,weight,height)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM