I am trying to make a contact list/phone book in python 3.10 with a dictionary and list and have been trying to add a feature to add a searched contact that isn't inside of the phone book. The current method I am trying gives me "Runtime Error: dictionary changed size during iteration", so do I need to find a way to append the dictionary without a for loop, or does anyone have any suggestions? I'm sorry if this is simple or I make little since, I am just starting to independently learn how to code. Thank you for any help provided. Here's the part where the error comes from:
from collections import defaultdict
#contact list using collection module
book = defaultdict(list)
search = input('Enter the name of the person you are looking for: ')
for key, value in book.items():
if key.startswith(search):
print(key, value)
else:
new_contact = input('That person is not in your contacts. Would you like to add them?(yes = y and no = n)')
if new_contact == 'y':
add_info = input('What is their contact information?')
book[search].append(add_info)
else:
break