簡體   English   中英

通過在Python中輸入員工姓名來打印員工的詳細信息

[英]Print details of an employee by entering the name of employee in Python

我正在從用戶那里輸入員工的詳細信息,然后要求用戶輸入員工的姓名,他想檢索信息。 我將所有輸入的數據保存在字典中,以員工姓名為鍵,[employee_id, salary, address] 為值。 現在無論我輸入什么名稱來檢索詳細信息,output 始終是最后輸入的員工的詳細信息。

    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

我想獲取 output 作為輸入姓名的員工的詳細信息。 請你能幫我擺脫這種情況嗎?

您沒有打印從字典中獲得的信息。 您需要從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('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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM