I am getting input of details of the employee from the user and then asking the user to enter the name of the employee, he wants to retrieve information of. I am saving all the entered data in a dictionary with employee name as its key and [employee_id, salary, address] as the values. Now whatever name I enter to retrieve the details, the output is always the details of the last entered employee.
n = int(input('Enter the number of employee: '))
employees = {}
for i in range(n):
name = input('Enter the name of the employee: ')
emp_id = input("Enter employee Id: ")
sal = int(input("Enter the employee salary: "))
address = input('Enter the employee address: ')
employees[name] = [emp_id, sal, address]
while True:
name = input('Enter employee name: ')
info = employees.get(name, -1)
if info == -1:
print('Employee does not exist')
else:
print('Employee details are: \n Employee Id: ', emp_id, '\n Salary: ', sal, '\n Address: ', address)
exit_choice = input('Do you want to exit [Yes|No]: ')
if exit_choice == 'No' or exit_choice == 'no':
break
I want to get the output as the details of the employee whose name is entered. Please can you help me get out of this situation?
CodePudding user response:
You're not printing the information you got from the dictionary. You need to get that from info.
while True:
name = input('Enter employee name: ')
info = employees.get(name, -1)
if info == -1:
print('Employee does not exist')
else:
emp_id sal, address = info
print('Employee details are: \n Employee Id: ', emp_id, '\n Salary: ', sal, '\n Address: ', address)
exit_choice = input('Do you want to exit [Yes|No]: ')
if exit_choice == 'No' or exit_choice == 'no':
break
CodePudding user response:
I would do the following:
while True:
name = input('Enter employee name: ')
info = employees.get(name, -1)
if info == -1:
print('Employee does not exist')
else:
print(f'Employee details are: \n Employee Id: {info[0]} \n Salary: {info[1]} \n Address: {info[2]}')
#And then the exit thing
CodePudding user response:
a lots of bug in your code, the correct code will be,
n = int(input('Enter the number of employee: '))
employees = {}
for i in range(n):
name = input('Enter the name of the employee: ')
emp_id = input("Enter employee Id: ")
sal = int(input("Enter the employee salary: "))
address = input('Enter the employee address: ')
employees[name] = [emp_id, sal, address]
while True:
name = input('Enter employee name: ')
info = employees.get(name, -1)
if info == -1:
print('Employee does not exist')
else:
print(f'Employee details are: \n Employee Id: {info[0]}\n Salary: {info[1]}\n Address: info[2])')
exit_choice = input('Do you want to exit [Yes|No]: ')
if exit_choice == 'Yes' or exit_choice == 'yes':
break
