简体   繁体   中英

Creating a while loop within a class and adding user input to list?

*****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.

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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