![]() |
Learning python, stuck on some code. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Learning python, stuck on some code. (/thread-17200.html) |
Learning python, stuck on some code. - stanceworksv8 - Apr-02-2019 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...? RE: Learning python, stuck on some code. - ichabod801 - Apr-02-2019 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.
RE: Learning python, stuck on some code. - stanceworksv8 - Apr-02-2019 (Apr-02-2019, 12:43 AM)ichabod801 Wrote: You want a 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!! |