0

*****UPDATE: thanks to help, figured out while loop, struggling to print list with the animal type and name that the user inputted within the list.******

# Define your pet Class
# Setup attributes for the type of animal and a name
# Include type of animal and name in the __init__ method

class petClass:
    def __init__(self, animal, name):
        self.animal = animal
        self.name = name

# Create an empty List to store your pet objects

myPetObjects = []

# Create a While loop that continues to ask the user for the information for a new pet object
# Within the loop, create new pet objects with the inputted name and animal type.
# Add this new pet to our pet List
# Ask if the user wants to add more.  If not, exit the While loop

while True:
new_pet = petClass(input("what type of animal"), input("what is its name"))
myPetObjects.append(new_pet)
response = input("add another animal?")
if response == 'no':
    break 

# Create a new For loop that goes through your pet List.
# Print out the animal type and name of the pets within your List
for pet in myPetObjects:
    print(myPetObjects)

the output of the printed list is not what I'm looking for, just looking for the names and animal type.

2
  • 3
    You can simply use while True if you have a condition to break from it. Commented Oct 23, 2019 at 2:46
  • 2
    There is no need to edit the question with "SOLVED" or "UPDATE: <answer>". It is enough to upvote or accept the answer that helped you. Remember that this Q&A isn't for you alone. Someone else could encounter the same issue, and they can just get the solution from the accepted or highly voted answer. Commented Oct 23, 2019 at 4:38

2 Answers 2

2

Here's what I've noticed with this code.

First of all your line:

if input = no:

should be changed to

if response == 'no':

I have changed your name "input" to "response" because input is a built-in function in python, and we should not use it as a variable name.

The == operator checks for equivalence of its operands, in this case input and 'no'. The single equals = is the assignment operator in python, meaning that it stores the value on the right side of the = into the name given on the left. For example,

myPetObjects = []

assigns an empty list on the right to the name myPetObjects on the left.

The other change in the code you may notice in the line I rewrote is the quotations added to 'no'. This will compare the value referred to by the name "input" to the string literal 'no'. As previously written, "no" without quotes refers to a name/variable no, which has not been defined and will confuse Python.

Another thing: Since you are using a break statement, which will leave your loop once ran, you can update the while loop line to simply say

while True:

This way, the loop will continue indefinitely until explicitly exited with a "break" statement (which you have included).

As for the actual body of the while loop, it needs a little refactoring. The statement

input("what type of animal")

is a call to the built-in input function of python, and it will evaluate to, or return, a string inputted by the user when prompted with "what type of animal".

So, we can treat it as a string. With this in mind, let's create your pet objects. Creating an object in Python has the general syntax

object_name = class_name(parameters)

where parameters refers to the parameters taken by __init__. This will assign an object instance of your class class_name to object_name by using the assignment operator =.

To append a new object to myPetObjects, we do not need to assign our new object a name. We can instead do:

myPetObjects.append(class_name(parameters))

In your situation, this can be written:

myPetObjects.append(petClass(input("what type of animal"), input("what is its name")))

So, our entire loop:

while True:
    myPetObjects.append(petClass(input("what type of animal"), input("what is its name")))
    response = input("add another animal?")
    if response == 'no':
        break 

Perhaps a more easily readable version:

while True:
    new_pet = petClass(input("what type of animal"), input("what is its name"))
    myPetObjects.append(new_pet)
    response = input("add another animal?")
    if response == 'no':
        break 

I hope this helps out, and let me know if you have any more questions, if something wasn't clear, or if I didn't answer the question the way you wanted. Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

That was more help than I thought I could find! Thank you. The only thing I have a question on is how to print the list that would read the type of animal and name that I inputted. I remember in my class that when working with classes, lists don't print the same?
oh, yeah! I forgot that part. To print an object's attribute, do print(object.attribute). In this instance, you can access the objects by accessing the list's members. I'll post the code below, hope it helps.
for x in myPetObjects: print(x.name, x.animal)
0

By providing true or false to the while condition you can control the iteration:

#prompting user to enter inputs to the object
while answer != 'no':
    animalInput = input('What type of animal: ')
    nameInput = input('what is its name: ')

    #creating instances and adding to the list
    myPetObjects.append(petClass(animalInput, nameInput))

    #Adding more pets into object
    answer = input("Do you want to add more(yes/no): ")

    if answer == "yes":
        # Do this again

    elif answer == "no":
    break

#print all            
for pet in myPetObjects:
     print(pet.animal +":"+ pet.name)

1 Comment

Sorry, It should be printed after the while loop, thank you

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.