We will create an employee database app using nested dictionary objects, custom functions. This is part 1.
dEmp = {
"emp1": {
"fname" : "Bill",
"lname" : "Moseley",
"age" : 49,
"email" : "bill@bmoseley.com",
"salary" : 98753.27
},
"emp2": {
"fname" : "Dave",
"lname" : "Smith",
"age" : 25,
"email" : "dsmith@bmoseley.com",
"salary" : 45753.27
}
}
def showMenu():
print("""
1. Add an Employee
2. Terminate an Employee
""")
def getID(emp_data):
if not emp_data:
return "emp1"
emp_numbers = [int(key[3:]) for key in emp_data.keys() if key.startswith("emp")]
next_id = max(emp_numbers, default=0) + 1
return f"emp{next_id}"
def addEmployee(emp_data):
strFName = input("What is the first name? ")
strLName = input("What is the last name? ")
intAge = int(input("What is the age? "))
strEmail = input("What is the email? ")
flSalary = float(input("What is the salary? "))
dInner = {"fname":strFName, "lname":strLName, "age":intAge, "email":strEmail, "salary": flSalary}
strID = getID(emp_data)
emp_data[strID] = dInner
print(f"New Employee Added: {strFName} {strLName}, ID: {strID}")
return emp_data
print("*"*60)
print("*"+"Employee Database".center(58)+"*")
print("*"*60)
print("")
showMenu()
strMenu = input("What would you like to do? (Choose 1-2): ")
if strMenu == "1":
addEmployee(dEmp)
elif strMenu == "2":
pass
else:
print("You need to enter a number for the menu choice.\n")