I'm trying to make simple code that reads names and put it on a list, what i am intending to do is a series of functions to add names to the list, remove name, find name position etc. so it will have a lot of ifs, to avoid that i tried to use a dictionary to store the functions that will be called, but when i execute the program the functions inside the dictionary are running before i can input the number to execute it
def addName(nameListAux):
"""Returns a list of names
Args:
aux str: name that will add to the list list
"""
name = str(input('Enter a new name: '))
while name != 'quit':
nameListAux.append(name)
print(nameListAux)
name = str(input('Enter a new name: '))
print(nameListAux)
return nameListAux
def searchName(searchedNameList):
"""Search a name on the list
Args:
searchedNameList list: list that will search the name
Returns:
int: position of the name in the list
"""
name = str(input('Enter name to be searched: '))
listLength = (int(len(searchedNameList)) // 2) - 1
while listLength <= (int(len(searchedNameList) - 1)):
if name == searchedNameList[listLength]:
return print('the name is at position', listLength)
listLength += 1
listLength = (int(len(searchedNameList)) // 2) - 1
while listLength >= 0:
if name == searchedNameList[listLength]:
return print('the name is at position', listLength)
listLength -= 1
if __name__ == '__main__':
nameList = []
selectionDict = {
1: addName(nameList),
2: searchName(nameList),
}
while True:
switch = int(input('''Choose a option:
1- Add name to list
2- Search for name in list
'''))