Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Learning python, stuck on some code.
#1
Hello everyone,
I have been learning Python for a few weeks now and I am now doing a project to create a employee management system. I got to the point of creating a branching path for the user to either select 1 to add employee or 2 to view the list of employees. The problem is after I select 1 and add one employee the program stops... I want it to go back to the screen where it asks "1 or 2" so it can ask for user input every time the user adds or views the lists. I don't want it to end... I have been looking and looking for hours at different videos of creating loops etc and I either get a syntax error or some crazy non stop printing that I cant stop unless I kill the program...
My code is:
counter = 0
def main():
    global counter
print("Employee Management System\n--------------------------------------")
print("There are", main(), "employees in the system.")
print("\n")
print("Please select from the following options:")
#----------------#1 function to add employee-----------------------------
employees =[]
names = []
def add_em():
    name = input('Employee Name:')
    ssn = input('Employee SSN:')
    phone = input('Employee Phone No.:')
    email = input('Employee Email:')
    salary = input('Employee Salary: $')
    line = (name + ssn + phone + email + salary)
    names.insert(counter, name)
    employees.insert(counter,line)
counter =counter+1
#-----------------#2 function to view the list----------------------------    
def view_em():
    if not names:
        print("Sorry there are no employees in the list")
    else:
        print(counter,names)
#----------------- Asks user for option 1 or 2 ----------------------------
a = input("Press 1: Add an employee \nPress 2: View employee list\nInput: ")
if a == "1":
    add_em()
elif a == "2":
    view_em()
# How do I make this loop back to "a = input..." so the user can choose again
# between 1 or 2...?
Quote
#2
You want a while True: loop. Put lines 28-32 inside that. Then have a third option to quit. When that option is selected, use a break statement to get out of the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(Apr-02-2019, 12:43 AM)ichabod801 Wrote: You want a while True: loop. Put lines 28-32 inside that. Then have a third option to quit. When that option is selected, use a break statement to get out of the loop.

Thank you so much, that worked so beautifully.
I was putting the while True: loop in the wrong space, I was adding it at the end of everything and it was giving me errors. I watched so many videos and tutorials, I was close but couldnt get it right.
Thanks again!!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  I'm teaching myself python and i'm stuck... stormrider 2 1,028 Dec-06-2017, 10:16 PM
Last Post: Larz60+
  Still learning - code efficiency, which of these is better? shelzmike 2 987 Oct-14-2017, 04:47 AM
Last Post: shelzmike
  I finsh the basics of python but I'm stuck on how to link code to GUI Qubayel 5 1,508 Apr-04-2017, 07:18 AM
Last Post: Ofnuts

Forum Jump:


Users browsing this thread: 1 Guest(s)