0

I am trying to figure out what I continue to do wrong here with my code. I have tried changing the second portion by eliminating everything after # The main function. Thoughts?

I get the following error

Traceback (most recent call last): File "file", line 1, in import petspgm File "file", line 39, in main() File "file", line 34, in main print('Pet Name: ', pets.get_pet_name) builtins.UnboundLocalError: local variable 'pets' referenced before assignment –

import petspgm

def main():
    #Get a list of pet objects.
    pets = make_list()

    #Display the data in the list.
    print('Here is the data you entered: ')
    display_list(pets)

#This function gets data from the user for 3 pets. It returns a list of pet
#objects containing the data.
def make_list():
    #Create an
    pet_list = []

    #Add three pet objects to the list
    print('Enter the data for 3 pets.')
    for count in range(1, 3):
        #Get the pet data
        print("Pet number " + str(count) + ":")
        pet_name = input("Enter your pet's name: ")
        pet_type = input("Enter the type of pet: ")
        pet_age = float(input("Enter the pet's age: "))

        #Create a new PetData object in memory and assign it to the pet variable.
        pet = petspgm.PetData(pet_name, pet_type, pet_age)

        #Add the object to the list.
        pet_list.append(pet)

        #Return the list.
        return pet_list

#This function accepts a list containing PetData objects as an argument and 

def pet_list():

    #displays the data stored in each object.
    display_list(pet_list)
    for item in pet_list:
        print("Pet's name is: " + item.get_pet_name())
        print("Pet's type is : " + item.get_pet_type())
        print("Pet's age is : " + item.get_pet__age())

#Call the main function
main()

Here is the other half to the code:

class PetData(object):
    def __init__(self, name, animal_type, age):
        self.__pet_name = pet_name
        self.__pet_type = pet_type
        self.__pet_age = pet_age

    def set_name(self, name):
        self.__pet_name = pet_name

    def set_type(self, animal_type):
        self.__pet_type = pet_type

    def set_age(self, age):
        self.__pet_age = pet_age

    def get_name(self):
        return self.__pet_name

    def get_animal_type(self):
        return self.__pet_type

    def get_age(self):
        return self.__pet_age


# The main function
def main():
    # Prompt user to enter name, type, and age of pet
    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: '))
    print('This will be added to the records. ')
    print('Here is the data you entered:')
    print('Pet Name: ', pets.get_pet_name)
    print('Animal Type: ', pets.get_pet_type)
    print('Age: ', pets.get_pet_age)
    pets = Pet(name, animal_type, age)

main()
3
  • 2
    Please provide more details. What is going wrong with your code? Is something happening that you expect not to happen? Is something that you expect to happen not happening? Be specific stackoverflow.com/help/how-to-ask Commented Apr 26, 2016 at 23:36
  • Traceback (most recent call last): File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\BakerVPR9.py", line 1, in <module> import petspgm File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\petspgm.py", line 39, in <module> main() File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\petspgm.py", line 34, in main print('Pet Name: ', pets.get_pet_name) builtins.UnboundLocalError: local variable 'pets' referenced before assignment Commented Apr 26, 2016 at 23:40
  • please update your original question with that data Commented Apr 26, 2016 at 23:41

1 Answer 1

1

In main:

print('This will be added to the records. ')
print('Here is the data you entered:')
print('Pet Name: ', pets.get_pet_name)
print('Animal Type: ', pets.get_pet_type)
print('Age: ', pets.get_pet_age)
pets = Pet(name, animal_type, age)

you print the pets values before actually initializing / declaring it. I think you want:

    pets = Pet(name, animal_type, age)
    print('This will be added to the records. ')
    print('Here is the data you entered:')
    print('Pet Name: ', pets.get_pet_name)
    print('Animal Type: ', pets.get_pet_type)
    print('Age: ', pets.get_pet_age)
Sign up to request clarification or add additional context in comments.

5 Comments

Now my error is giving me Traceback (most recent call last): File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\BakerVPR9.py", line 1, in <module> import petspgm File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\petspgm.py", line 40, in <module> main() File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\petspgm.py", line 32, in main pets = Pet(name, animal_type, age) builtins.NameError: name 'Pet' is not defined
@bakervin730 Have a look at the file and what classes you define. Why do you think Pet is not found?
@viraptor is it because it was Petdata previously, and I forgot Data from that?
@viraptor that solved that issue, but another has come to light... ... Here is the data you entered: Traceback (most recent call last): File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\BakerVPR9.py", line 46, in <module> main() File "C:\Users\BakerFamily\Documents\Vince\LCC\CITP 110\BakerVPR9.py", line 9, in <module> display_list(pets) builtins.NameError: name 'display_list' is not defined
display_list(pet_list) will not work because the function display_list is not a built in function, you need to define it yourself. perhaps just do print pet_list instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.