beginner coder here learning Python.
I am creating an employee management system project. The basic function below is searching for an employee profile by SSN.
Issue: The Else statement in the function below will sometimes not work. When I first run the program, it will find and display the first profile in the list. If I add another employee (separate function), and I search for that SSN, the program will run the Else statement stating it is not found. Then if I continue to exit the program (search again? No -> return to main menu? No -> end program), it will print the second profile I was searching for and start at that point again instead of exiting the program. I have printed my employee_list and confirmed that the information is properly formatted (each profile is a string separated by commas).
Sometimes, it works perfectly; that's whats tripping me up. If I knock out the else statement with #s, then I have zero issues searching. It seems to only happen after I manually add a profile, but if my profiles are imported from my .txt file, those search/work just fine. But again, I have printed my list multiple times and every entry, no matter how it got there, is formatted the same.
I have tried this in Python IDLE and in PyCharm with the same results. What am I missing? I am also getting inconsistent writes to .txt files (sometimes it writes everything in the list on 1 line, sometimes on multiple lines, sometimes with a empty row in between items). I mention that in case it could be indicative of my Python itself being messed up.
I know my code could be written better, but I just don't understand why these issues are occurring.
Thanks for any assistance.
employee_list = ['Adam,111-11-1111', 'Tom,222-22-2222']
def ssn_search():
print('---------- Search by SSN ----------')
ssn_select = input('Please enter employee SSN: ')
for i in range(0, len(employee_list)):
employee = employee_list[i].split(',')
if ssn_select == employee[1]:
print('Name:', employee[0])
print('SSN:', employee[1])
break
else: #<-------This else statement is the problem
print('That SSN does not exist in this system.')
print('Please try another SSN.')
ssn_again()
search_again = input('Would you like to search for another profile? Y/N: ')
if search_again in ['Y', 'y', 'Yes', 'yes']:
ssn_again()
elif search_again in ['N', 'n', 'No', 'no']:
ssn_edit = input('Would you like to edit this profile? Y/N: ')
if ssn_edit in ['Y', 'y', 'Yes', 'yes']:
edit()
elif ssn_edit in ['N', 'n', 'No', 'no']:
again()
else:
print('That is an invalid selection.')
else:
print('That is an invalid selection.')
ssn_again()```