Home > Blockchain >  How to create multiple objects using a class, where the objects' attributes are inserted by the
How to create multiple objects using a class, where the objects' attributes are inserted by the

Time:01-05

I think I have the basic idea down but I can't really make it work properly. I want to create 5 different objects with their own attributes, 3 each but they have to be inputs. They have to be created using another function which then returns them to show the user and also puts them in a list. This is what I have but and I can't figure out how to make it work:


class SmartPhone:
    def __init__(self, manufacturer, model, cost):
        self.manufacturer = manufacturer
        self.model = model
        self.cost = cost


manufacturer = input("Εισήγαγε τον κατασευαστή: ")
model = input("Εισήγαγε το μοντέλο: ")
cost = input("Εισήγαγε την λιανική τιμή: ")


def smart_phones(manufacturer, model, cost):
    smartPhones = []
    phone = SmartPhone(manufacturer, model, cost)
    smartPhones.append(phone)
    return "Smartphone: "   str(phone), smartPhones


smart_phones = smart_phones(manufacturer, model, cost)
for i in range(0, 5):
    print(smart_phones)

The problem lies in the smart_phones function, basically.

CodePudding user response:

You are appending an object of class SmartPhone in your list. That's why print() is returning you the address of the object in the memory. Maybe you want to append a list of manufacturer,model,cost . Something like this:

def smart_phones(manufacturer, model, cost):
   smartPhones = []
   phone = SmartPhone(manufacturer, model, cost)
   smartPhones.append([phone.manufacturer,phone.model,phone.cost])
   return "Smartphone: "    str(smartPhones)

Also, you are getting your inputs only once. To create 5 objects do this:

for i in range(0, 5):
   manufacturer = input("Creator:")
   model = input("Models:")
   cost = input("Cost:")
   print(smart_phones(manufacturer, model, cost))

CodePudding user response:

Not sure I fully understood your description but I'll try to help.

If you're trying to get 5 different SmartPhone objects, you will need to ask the user for 15 inputs (3 for each different phone). In your code you seem to ask the 3 fields for only once. You're essentialy printing the same object (with the same 3 fields) for 5 times.

Try something like this:

class SmartPhone:
    def __init__(self, manufacturer, model, cost):
        self.manufacturer = manufacturer
        self.model = model
        self.cost = cost

smartPhones = []

def smart_phones(manufacturer, model, cost):
    phone = SmartPhone(manufacturer, model, cost)
    smartPhones.append(phone)
    return "Smartphone: "   str(phone), smartPhones

for i in range(0, 5):
    smart_phones(input("Εισήγαγε τον κατασευαστή: "),input("Εισήγαγε το μοντέλο: "),input("Εισήγαγε την λιανική τιμή: "))
    
for i in range(1, 6):
    print("Phone "   str(i))
    print("Manufacturer: "   smartPhones[i].manufacturer   " ")
    print("model: "   smartPhones[i].model   " ")
    print("cost: "   smartPhones[i].cost   "\n")

Basicly this way you enter each 3 values needed for each phone. And instead of printing the place of the objects in the memory, you print each of the values within each loop.

CodePudding user response:

If you want to create a SmartPhone object, a class method is a good place to encapsulate the necessary I/O.

class SmartPhone:
    def __init__(self, manufacturer, model, cost):
        self.manufacturer = manufacturer
        self.model = model
        self.cost = cost

    @classmethod
    def from_user(cls):
        manufacturer = input("Εισήγαγε τον κατασευαστή: ")
        model = input("Εισήγαγε το μοντέλο: ")
        cost = input("Εισήγαγε την λιανική τιμή: ")
        return cls(manufacturer, model, cost)


smart_phones = [SmartPhone.from_user() for _ in range(5)]

CodePudding user response:

To print an object in an understandable way, you need to define either __repr__ or __str__ methods.

A simple example:

class SmartPhone:
    def __init__(self, manufacturer, model, cost):
        self.manufacturer = manufacturer
        self.model = model
        self.cost = cost
        
    def __repr__(self):
        return f"A ${self.cost} {self.__class__.__name__}: {self.manufacturer} {self.model}"

Define a couple of examples:

x=SmartPhone('Apple', '13', 1200)    
y=SmartPhone('Samsung', 'Galaxy', 900) 

Then make a list of those:

li=[x,y]

Then you can print at will:

>>> print(li)
[A $1200 SmartPhone: Apple 13, A $900 SmartPhone: Samsung Galaxy]

>>> x
A $1200 SmartPhone: Apple 13
  •  Tags:  
  • Related