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.