0

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.

4
  • Stepanjan's answer is accurate for the dynamically-sized array issue, but in terms of Python3 code rules, it is recommended to declare classes as: class ClassName(object):, where object is the type that ClassName sub-classes or implments (which may actually be object, in most cases). In general, it is also recommended to follow the PEP8 guidance. This is easiest followed if you use a text-editor or IDE that supports PEP8 (e.g., Atom with some community plugins and pip packages). Commented Oct 3, 2018 at 2:06
  • Should i use an object here? May i ask your opinion to what i should name my object if i am to add one? Commented Oct 3, 2018 at 2:15
  • IMO, no. If the pastebin is your full code, then an array is wholly sufficient for your use-case. Some of the more "OOP-everywhere" community members might disagree, since a class gives you the ability to extend and add data/fields later (e.g., maybe you will add a "Description" member to describe the type of content stored in the inventory), but as it stands right now, putting that in a class is overkill, in my opinion. Commented Oct 3, 2018 at 2:37
  • I see. Thanks for the info. Commented Oct 3, 2018 at 3:24

3 Answers 3

1
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) 
Sign up to request clarification or add additional context in comments.

3 Comments

Your answer would be better if you included a small description of what you did differently. Otherwise the reader has to compare your code to the original line-by-line and character-by-character.
i'd like to add that i need my inventory to be in order. Isn't dictionary and set both random (not in order)?
@Geni-sama, an array, which is what Stepanjan is suggesting, is ordered based on the order the user entered the items.
0

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

2 Comments

are the added instances in order? aren't dictionaries and sets both random (not in order) when it comes to adding values?
I have used list thus, it will append in order. But if you use dictionary or set it will be random.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.