I'm having difficulty figuring out how to append two user inputs into a list as a tuple. I would also like to call the list and enumerate the list (e.g., 0. dog_name:dog_breed). I'm not sure how to call display or if it is even written properly.

Here's what I have so far:

""" Acquire Dog name and breed from user and bind to list """

import sys

class Dog:
	
	def __init__(self, name, breed):
		self.name = name
		self.breed = breed
	
	def __str__(self):
		return "%s:%s" % (self.name, self.breed)
		
	def display(self):
		for i in enumerate(self):
			print(i, "%s:%s" % self.name, self.breed)

if __name__ == "__main__":
	dogs = list()
	first_inp = "Name: "
	second_inp = "Breed: "
	while True:
		dog_name = input(first_inp).strip()
		dog_breed = input(second_inp).strip()
		dogs = Dog(dog_name, dog_breed)
		print(dogs)

Any help would be greatly appreciated.

Recommended Answers

All 4 Replies

I believe this is what you are looking for.

dog_name = 'charlie'
dog_bread = 'pug'
dogs = []
dogs.append((dog_name, dog_breed))
print(dogs)

to get the data

Dog1 = dog[0]
Dog1Name = Dog1[0]
Dog1Breed = Dog1[1]

print(Dog1, Dog1Name, Dog1Breed)

Thank you, but I still receive:

dogs = dogs.append(('dog_name', 'dog_breed'))
AttributeError: 'Dog' object has no attribute 'append'

You will need to include this def in your Dog class.
The call it for appending
Also it will be a good idea to make self.dogApp,self.name and self.breed global variable access in the class but should work like this anyway.

def dppend(self)
   
  self.dogApp=[]
  self.dogApp.append((self.name,self.breed))
  return self.dogApp

Then call it like this

dogs= Dog(dogname,dogbreed)
dogs.dappend()

I'm not sure I understand your question but if you want to use multiple class instances, one per dog, try something like this.

if __name__ == "__main__":
	dogs = list()
        dog_name = ""
	first_inp = "Name: ('quit' to exit) "
	second_inp = "Breed: "
	while dog_name.lower() != "quit":
		dog_name = input(first_inp).strip()
		dog_breed = input(second_inp).strip()
		this_instance = Dog(dog_name, dog_breed)
                dogs.append(this_instance)
        for each_instance in dogs:
            print(each_instance.name, each_instance.breed)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.