0

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()```
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Visucius
  • 3
  • 1
  • Unrelated to your problem, but that `for` loop, why don't you just do something like `for employee in employee_list:`? Then you can do `name, ssn = employee.split(',')` That `range` and list-indexing isn't needed. – Some programmer dude Jun 26 '20 at 05:51
  • 1
    Can you produce an *exact* sequence of inputs that causes the problem, so that others can attempt to reproduce it? And explain *exactly* how what actually happens, differs from what should happen, specifically for that sequence? – Karl Knechtel Jun 26 '20 at 05:52
  • 2
    Actually - where is `ssn_again`, `edit` and `again`? I *suspect* what's going on is that you have convoluted logic where functions call each other in a cycle and you aren't thinking about what happens when those function calls return; but it's hard to say without actually seeing the relevant code. – Karl Knechtel Jun 26 '20 at 05:53
  • Other factors (cite Karl _"Actually - where is `ssn_again`, `edit` and `again`?"_) notwithstanding , it _seems_ that your `else` does not belong to the `if` block` but to the `for` block — please see this [question](https://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops) and the accepted answer for details – gboffi Jun 26 '20 at 06:09
  • Well, I did cut some of the code out so I wasn't posting a giant wall of text (more than I already did). – Visucius Jun 26 '20 at 18:33
  • Wouldn't let me edit the previous comment. Karl: ssn_again() just points back to ssn_search(). I agree that the logic of the functions may not be great, but from my understanding, they should still technically work; pretty much all functions end with calling back to other functions to get back to the main menu or wherever. gboffi: I see what you mean about it being in the wrong spot. I will try adjusting that. Some programmer dude: I will also try that out. Thanks everyone! – Visucius Jun 26 '20 at 18:40

1 Answers1

0

the problem is the else statement being inside the for loop. If you print out your employee[1] value, you will see that it selects Adam's ssn from the list, and then checks the ssn value, and goes into the else statement if you selected ssn = '222-22-2222'.

You want to loop and check both of them, therefore you need to change your code so that the else statement is outside of the for loop, so the if statement loops through for all the values before moving onto the else, hope that helps!

  • In your `if` **outside** of the loop, what are you checking for, if the employees' SSN numbers are found **inside** the loop? (I don't say it can't be done, but your answer as is it's not useful, you should improve it if you really want to help) – gboffi Jun 26 '20 at 06:50