I have a request to collect user input as dictionary key/value. If the key does not exist in current dictionary(dic1), add key/value into current dictionary(dic1). If the key does exist, create a new dictionary(dic2) and a new list(list1), add both dic1 and dic2 to the list, overwrite dic1 by dic2 to allow script works on dic1, if the newly entered key does not exist in dic2, update dic2. If the key does exist in dic2, do the same as above step. Continue above steps until user stop entering. Here is my code. If I have line (dic1=dic2.copy()), the IDE throw error saying cannot reference the dic1. I could not figure out the problem, can anyone point me to the right direction?
dic1 = {}
dic2 = {}
list1 = []
def collectinfo(continue_collect):
while continue_collect == "yes":
continue_collect = input("\n\rEnter yes to continue and no to stop: ")
inputkey = input("\n\rEnter key: ")
inputvalue = input("\n\rEnter value: ")
if inputkey not in list(dic1.keys()):
dic1[inputkey] = inputvalue
else:
list1.append(dic1)
dic2[inputkey]=inputvalue
dic1=dic2.copy()
return collectinfo("yes")