I am learning python at the moment the task is this; Write a program that reads pet information (name, type and age) from a file (called animals.txt) and creates Pet objects (using the information stored in the animals.txt file). Store Pet objects in a list called animals.
animal.txt
ralph, dog, 3
buster, cat, 8
sammy, bird, 5
mac, dog, 1
coco, cat, 6
My class file i created is called pet.py
class Pet:
# The __init__ method initializes the data attributes of the Profile class
def __init__(self, name ='', animal_type = '', age = ''):
self.__name = name
self.__animal_type = animal_type
self.age = 0
def __str__(self):
string = self.__name + ' ' + self.__animal_type + ' ' + self.age
return string
def set_name(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_animal_type(self, breed):
self.__animal_type = breed
def get_animal_type(self):
return self.__animal_type
def set_age(self, old):
self.age = old
def get_age(self):
return self.age
I then want to use this class in a file animals.py
import pet
animals = [] // create a list
infile = open("animals.txt", "r") // open the file
lines = infile.readlines() // read all lines into list
## add each pet object
for line in lines:
data = line.split(",")
animals.append(pet.set_name(data[0]))
animals.append(pet.set_animal_type(data[1]))
animals.append(pet.set_age(data[2]))
infile.close()
I am getting an error
pet.set_name [pylint] E1101 : module 'pet' has no 'set_name' member.
If i do this code below in the class file pet.py I don't get the error
pet = Pet()
name = "thing"
breed = "dog"
pet.set_name(name)
pet.set_animal_type(breed)
pet.set_age(10)
print(pet)
and it returns as expected
thing dog 10
Why wont the animals.py file allow me to use the class i have imported?
I have tried pet=Pet() but it has
error E0602: undefined variable 'Pet'
from pet import Petthen usePetas you did before.