Geni-sama
Geni-sama

Reputation: 317

How do I create instances in class using user's input?

I am trying to create any number of instances in a class depending on the user's input but so far I unable to:

class CLASS_INVENTORY:
    maxcount_inventory = int(input("How many Inventories: "))
    for count_inventory in range(maxcount_inventory): 
        def __init__(Function_Inventory, inventory_name(count_inventory)):
            add_inventory = str(input("Enter Inventory #%d: " % (count_inventory+1)))
            inventory_name[count_inventory] = add_inventory   

Note: I'm kind of new in Python 3 so I'm not sure if some of my syntax are right.

I want the output to be like this:

How many Inventories: 4
Enter Inventory #1: Fruits
Enter Inventory #2: Veggies
Enter Inventory #3: Drinks
Enter Inventory #4: Desserts

Here's my full code: https://pastebin.com/3FBHgP6i I'd also like to know the rules in writing Python 3 code if I'm following them right or I should change something. I'd like it to be readable as much as possible for other Python programmers.

Upvotes: 0

Views: 120

Answers (3)

vash_the_stampede
vash_the_stampede

Reputation: 4606

You can construct your dicitonary within def _init__(self) then set up a separate method print_inventories with a loop to print while maintaining the order of entry

class Inventory():
    def __init__(self):
        self.inventories = {}
        n = int(input('How many inventories: '))
        for i in range(1, n+1):
            self.inventories[i] = input('Enter inventory #{}: '.format(i))

    def print_inventories(self):
        for k in self.inventories:
            print('#{}: {}'.format(k, self.inventories[k]))

a = Inventory()
a.print_inventories()
How many inventories: 4
Enter inventory #1: Fruits
Enter inventory #2: Veggies
Enter inventory #3: Drinks
Enter inventory #4: Desserts
#1: Fruits
#2: Veggies
#3: Drinks
#4: Desserts

Upvotes: 0

Bhushan Pant
Bhushan Pant

Reputation: 1580

I would do something like this:

# Define class
class CLASS_INVENTORY():
   # On making a class object init will be called
   def __init__(self):
       # It will ask for the inventory type count
       self.maxcount_inventory = int(input("How many Inventories: "))
       self.inventory = []
       # Just loop around it to get the desired result
       for count_inventory in range(0, self.maxcount_inventory):
           add_inventory = str(input("Enter Inventory #%d: " % (count_inventory+1)))
           self.inventory.append(add_inventory)

Output:

CLASS_INVENTORY()
How many Inventories: >? 2
Enter Inventory #1: >? Apple
Enter Inventory #2: >? Orange

Upvotes: 0

Ararat Stepanjan
Ararat Stepanjan

Reputation: 75

class CLASS_INVENTORY():
    maxcount_inventory = int(input("How many Inventories: "))
    inventory=[]
    def __init__(self):
        for count_inventory in range(0, self.maxcount_inventory):
            add_inventory = str(input("Enter Inventory #%d: " % (count_inventory+1)))
            self.inventory.append(add_inventory) 

Upvotes: 1

Related Questions