0

I'm new to python and am trying to implement some of the new things I've learnt over my course and am trying to define a dictionary class that has a function that allows it to be iterated through and displayed however when compiled and ran, I get a TypeError: object is not iterable.

I'm working on pycharm and can't seem to figure out what the problem is. I've been able to get rid of the error through rewriting some of the code however that just results in no output.

class Dict:
    def __init__(self):
        self.UserDict = {}

    def __setitem__(self, key, item):
        self.__dict__[key] = item

    # def __getitem__(self, key):
        # self.__dict__[key]

    def KeyandVal(self, dic, key, val):
        dic[key] = val

    def SwapVal(self, dic, key1, key2):
        temp = ""
        temp = dic[key1]
        dic[key1] = dic[key2]
        dic[key2] = temp

    def display(self, dic): 
        for dic.UserDict.key, dic.UserDict.value in dic.UserDict:
            print(dic.UserDict[dic.UserDict.key])




choice = 1
UserDict = Dict()
while True:
    print("------------------------------")
    print("")
    print("1. Create a key and value pair")
    print("2. Change the value of a key")
    print("3. Swap key values")
    print("4. Display Dictionary")
    print("5. Export data to new file")
    print("6. Logout")
    print("")
    choice = input("Please enter a number for the desired task or '0' to Exit: ")
    if choice == "0":
        print("Thank you!")
        time.sleep(1)
        break
    elif choice == "1":
        key = input("What would you like to name this key?: ")
        val = input("What would you like its value to be?: ")
        UserDict.KeyandVal(UserDict, key, val)
    elif choice == "2":
        key = input("Which key value would you like to change?: ")
        val = input("Enter new value: ")
        UserDict.KeyandVal(UserDict, key, val)
    elif choice == "3":
        key1 = input("Enter first key: ")
        key2 = input("Enter second key: ")
        UserDict.SwapVal(UserDict, key1, key2)
        print("Values Swaped!")
    elif choice == "4":
        UserDict.display(UserDict)

Preferably, once the display function is ran, the key and value pairs should be outputted. Again, nothing is outputted instead.

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
  • Paste the full traceback. The traceback provides really helpful information (e.g. line numbers). So include those fully. – TrebledJ Jun 07 '19 at 19:17
  • It's very unclear what you're asking for. The general way to make thimething iterable is to give it an `__iter__` method. Have you tried that? If you did, please show us what you wrote and the full traceback of any exception it lead to. Your current code is very confusing, as it's not clear if you expect your `Dict` class to use its own `__dict__`, or the `UserDict` attribute you create for it in the `__init__` method. – Blckknght Jun 07 '19 at 19:23

1 Answers1

0

On the line for dic.UserDict.key, dic.UserDict.value in dic.UserDict:, specifically on in dic.UserDict you are implicitly calling the __iter__ method of your Dict class, which isn't implemented. That's why you are seeing this TypeError: object is not iterable.

For more info take a look at this method's docs.

Bonifacio2
  • 3,405
  • 6
  • 34
  • 54