Python Forum
Learning python, stuck on some code.
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...?
Reply
#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
Reply
#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!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner stuck in Python book for kids mic81k 11 1,124 Nov-27-2023, 04:28 AM
Last Post: deanhystad
Sad I'm stuck with the reinforcement learning function in my Code OskiLori 2 1,511 May-20-2023, 10:01 PM
Last Post: OskiLori
  Translation of R Code to Python for Statistical Learning Course SterlingAesir 2 2,092 Aug-27-2020, 08:46 AM
Last Post: ndc85430
  Stuck on python quiz challenge baesian 2 2,152 Aug-16-2020, 12:52 AM
Last Post: scidam
  learning to code python nhan 5 2,459 May-23-2020, 03:35 PM
Last Post: nhan
  I'm teaching myself python and i'm stuck... stormrider 2 2,977 Dec-06-2017, 10:16 PM
Last Post: Larz60+
  Still learning - code efficiency, which of these is better? shelzmike 2 3,244 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 4,298 Apr-04-2017, 07:18 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020