我正在从用户那里输入员工的详细信息,然后要求用户输入他想要检索信息的员工的姓名。我将所有输入的数据保存在字典中,以员工姓名作为键,以 [employee_id、salary、address] 作为值。现在,无论我输入什么名称来检索详细信息,输出始终是最后输入的员工的详细信息。
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
我想获得输出作为输入姓名的员工的详细信息。请问你能帮我摆脱这种困境吗?
你的代码中有很多错误,正确的代码是,
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
您没有打印从字典中获得的信息。您需要从
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
我会做以下事情:
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
n = int(input('请输入员工人数:')) 员工 = {}
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