1

How do i add a set of values in order (per inventory) from a user's input for e.g. {fruit:{1,2,3,4},{veggies:{5,6,7,8}}

Nothing i've tried so far ever worked.

Here's my code:

def Create_Inventory():

    clear()
    inventory_dictionary = {}

    count_inventory = int(input("Enter the number of Inventories: "))

    for num_inv in range(count_inventory):

        add_inventory = str(input("Enter Inventory #%d: " % (num_inv + 1)))
        inventory_dictionary[add_inventory] = set()

    for find_key in inventory_dictionary:
        count_item = int(input("\nHow many items in %s inventory: " % find_key))
        for num_item in range(count_item):
            #add_item = input("Enter item #%d: " % (num_item + 1))
            #inventory_dictionary[find_key].add(add_item)
            add_item = set(input("Enter item #%d: " % (num_item + 1)))
            inventory_dictionary[find_key].update(add_item)

My full code: https://pastebin.com/gu5DJX5K

jpp
  • 159,742
  • 34
  • 281
  • 339
Geni-sama
  • 307
  • 5
  • 15

1 Answers1

2

Sets are unordered collections. What you want is impossible. Consider these alternatives:

  • list, ordered but permits duplicates.
  • dict.keys in Python 3.6+, where dictionaries are insertion ordered.
  • OrderedDict.keys in Python versions prior to 3.6.
  • Create a custom class, e.g. this recipe.
  • Use a 3rd party library, e.g. ordered-set.

See also: Does Python have an ordered set?

jpp
  • 159,742
  • 34
  • 281
  • 339