[英]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.