0

I´m new to Python and I play a little bit with this language. Is it possible to create more then one object with a while-loop? I want to give the objects the same name+number, like: wuff = "object"+count or something?

So the first one is: object1

2cnd: object2

and so on

Is this even possible or am I forced have to use a list?

The class is:

class Animal:
    def __init__(self,name,alter,typ):
        self.typ = typ
        self.name = name
        self.alter = alter

Code:

count = int(input("Enter amount of Inputs: "))

try:
    while count>0:
        wuff = Animal(input("Enter Name: "), input("Enter Age: "), input("Enter Type: "))

>         wuff = "object"+count

        count-=1
except ValueError:
    print("Wrong input!")

print(wuff.name, wuff.alter, wuff.typ)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • *"am I forced to have to use a list"* ...what's wrong with lists? They are the natural data structure for representing *n* items. – Mateen Ulhaq Aug 14 '20 at 08:56
  • or use a dict. {"giraffe":Animal(...), "cat":Animal(..)} if you want it descriptive.You can create a dict using loops – Vishesh Mangla Aug 14 '20 at 08:57
  • There is nothing wrong with lists, I just want to try it without a list - if something is even possible. That´s the question :) – Hunterss Aug 14 '20 at 08:58
  • You can also use dictionaries, tupels, sets, frozensets. – Andreas Aug 14 '20 at 09:06
  • Not sure if sets can be used @Andreas. Objects are mutable but sets are not – Vishesh Mangla Aug 14 '20 at 09:20
  • @VisheshMangla Sets are mutable, and can contain mutable elements, so long as they are [hashable](https://stackoverflow.com/q/14535730/12299000). – kaya3 Aug 14 '20 at 10:09
  • @kaya3 thanks, I did see how sets are implemented internally by hash functions but still mess it up. I did mean this hashable behaviour only. – Vishesh Mangla Aug 14 '20 at 11:32

1 Answers1

0

Might I suggest a solution to not using lists:

Given that your class has structure :

class Animal:
    def __init__(self,name,alter,typ):
        self.typ = typ
        self.name = name
        self.alter = alter

In your main code, you can make some changes so that you don't need to use lists, i.e. change the class to :

class Animal:
    def __init__(self):
        self.typ = None
        self.name = None
        self.alter = None

This will basically allow you to declare a class object without having to set its attributes when declaring it. So, with the changes made in the class structure, your main code, i.e. this code:

count = int(input("Enter amount of Inputs: "))

try:
    while count>0:
        wuff = Animal(input("Enter Name: "), input("Enter Age: "), input("Enter Type: "))

          wuff = "object" + count

        count-=1
except ValueError:
    print("Wrong input!")

print(wuff.name, wuff.alter, wuff.typ)

We can change this to the following code:

count = int(input("Enter amount of Inputs: "))
wuff = Animal()
try:

    while count > 0:
        wuff.name = str(input("Enter Name: "))
        wuff.alter = int(input("Enter Age: ")) # assuming that you mean to set age by the attr. alter
        wuff.typ = str(input("Enter Type: "))
        # wuff = "object" + count # removed the line because it is incorrect
        # The code line was removed tries to add a string literal with an integer.
        count -= 1

except ValueError:
    print("Wrong input!")

print(wuff.name, wuff.alter, wuff.typ)

In order to create multiple objects, you need to have multiple identifiers. So you need to use some form of storing method, i.e. some sort of List or dictionary. So to do this :

class Animal:
    def __init__(self,name,alter,typ):
        self.typ = typ
        self.name = name
        self.alter = alter

count = int(input("Enter amount of Inputs: "))
objectDict = {}

try:
    while count > 0:

        Name = str(input("Enter Name: "))
        Alter = int(input("Enter Age: ")) 
        Typ = str(input("Enter Type: "))
        
        objectDict[Name] = Animal(Name, Alter, Typ)
        count -= 1

except ValueError:
    print("Wrong input!")

print(wuff.name, wuff.alter, wuff.typ)

I hope this was useful.

You can avoid lists (although not recommended) and stuff by storing the objects by using a SQL DB and storing the objects in the form of tables of attributes and stuff.

I hope this answer was satisfactory.

Novus Edge
  • 131
  • 1
  • 5