im trying to code a simple contact book with just names and phone numbers. I want it to have the options add,remove,search and exit obviously. In my remove and search option i got one problem: For example if the use adds a new name to the contact book for example "Alex Balex" and wants to remove it or search it after he needs to type Alex Balex but i want the program to find and delete Alex out of the list if i search for "Alex Bal" aswell. That was my attempt but i cant find a solution:
import pickle import sys while True:
option = int(input(("You got 3 options 1=add 2=remove 3=search 4=exit and save. Please enter what you want to do: ")))
if option == 1:
names = []
names = pickle.load(open("names.dat", "rb"))
new_entry, new_number = str(input("Enter a new name which you want to be added to the CB in this format name:phone_number\nINPUT: ")).split(":")
new_user = "Name: " + new_entry + " Number: " + new_number
names.append(new_user)
pickle.dump(names, open("names.dat", "wb"))
elif option == 2:
names = []
names = pickle.load(open("names.dat", "rb"))
print(names)
new_removal = str(input("Enter what you want to remove: "))
for element in names:
if new_removal in element:
names.remove(names[names.index(element)])
pickle.dump(names, open("names.dat", "wb"))
elif option == 4:
sys.exit()
elif option == 3:
names = []
names = pickle.load(open("names.dat", "rb"))
print(names)
new_search = str(input("Search either a name or a number: "))
for element in names:
if new_search in element:
new_search = names[names.index(element)]
print(new_search)
pickle.dump(names, open("names.dat", "wb"))