2017-10-29 3 views
-1

Hello StackOverflow 커뮤니티, 저장 용 사전을 구현하는 직원 데이터베이스 프로그램을 작성하는 작업이 거의 완료되었습니다. 내가 실행 해본 문제는 메뉴 프롬프트 이전에 저장된 초기 Employee 객체를 찾을 수 없다는 것입니다. 그것들은 데이터베이스에 존재하지만, 코드로는 전혀 조작 할 수 없습니다. 나중에 메뉴 기능을 사용하여 추가 한 항목은 조회하거나 삭제할 수 있습니다. 그 문제가 뭔지 잘 모르겠습니다. 나는 아무 쓸모없는 사전을 만들려고 노력했다. 내가 사용하는 진술은 거의 동일합니다.사전 조회가 초기 입력을 인식하지 못합니다.

class Employee: 
    def __init__(self, name, idNo, department, title): 
     self.name = name 
     self.idNo = idNo 
     self.department = department 
     self.title = title 

    def setName(newName): 
     self.name = newName 

    def setIDNo(newID): 
     self.idNo = newID 

    def setDepartment(newDept): 
     self.department = newDept 

    def setTitle(newTitle): 
     self.title = newTitle 

    def getName(): 
     return self.name 

    def getIDNo(): 
     return self.idNo 

    def getDepartment(): 
     return self.department 

    def getTitle(): 
     return self.title 

    def __str__(self): 
     return "Name: {0} \nID number: {1} \nDepartment: {2} \nTitle: {3}\n".format(self.name, self.idNo, self.department, self.title) 

def main(): 
    empDatabase = {} 
    entry = input("Do You Want to Enter New Employee Information(Y/N): ") 
    entry = entry.upper() 
    if entry == 'Y': 
     gate = True 
     newName = input("Enter Employee Name: ") 
     newID = input("Enter Employee ID Number: ") 
     newDept = input("Enter Employee Department Name: ") 
     newTitle = input("Enter Employee Job Title: ") 
     empDatabase[newID] = Employee(newName, newID, newDept, newTitle)  
     another = input("Do You Want to Enter Another Employee Information(Y/N): ") 
     another = another.upper() 
     if another != 'Y': 
      gate = False 
     while gate == True: 
      newName2 = input("Enter Employee Name: ") 
      newID2 = input("Enter Employee ID Number: ") 
      newDept2 = input("Enter Employee Department Name: ") 
      newTitle2 = input("Enter Employee Job Title: ") 
      empDatabase[newID2] = Employee(newName2, newID2, newDept2, newTitle2) 
      another2 = input("Do You Want to Enter Another Employee Information(Y/N): ") 
      another2 = another2.upper() 
      if another2 != 'Y': 
       gate = False 
    boolGate = True 
    while boolGate == True: 
     print("\nPlease Select from the Following Menu: ") 
     print("1. Look up an Employee in the Dictionary") 
     print("2. Add a New Employee to the Dictionary") 
     print("3. Delete an Employee from the Dictionary") 
     print("4. Quit the Program") 
     print() 
     choice = eval(input("Please Enter a Number from the Menu: ")) 
     if choice == 1: 
      idNo = eval(input("Please Enter the Employee ID You are Looking for: ")) 
      dic_lookup(idNo, empDatabase) 
     elif choice == 2: 
      empDatabase = dic_add(empDatabase) 
     elif choice == 3: 
      idNo = eval(input("Please Enter the Employee ID to Delete the Employee: ")) 
      dic_delete(idNo, empDatabase) 
     else: 
      boolGate = False 
def dic_lookup(idNo, database): 
    if idNo in database: 
     print() 
     print(database.get(idNo)) 
    else: 
     print() 
     print("This Employee ID is not Available in our Database") 
def dic_add(database): 
    print() 
    addName = input("Enter New Employee Name: ") 
    addID = eval(input("Enter New Employee ID Number: ")) 
    addDept = input("Enter New Employee Department Name: ") 
    addTitle = input("Enter New Employee Job Title: ") 
    database[addID] = Employee(addName, addID, addDept, addTitle) 
    return database 

def dic_delete(idNo, database): 
    if idNo in database.keys(): 
     database.pop(idNo) 
    else: 
     print() 
     print("Employee does not exist in database") 
    return database 

main() 
+0

기존 직원을 어디서로드합니까? – Vinny

+0

들여 쓰기를 수정하십시오. 어떤 함수가'Employee' 클래스에 속하는지 명확하지 않습니다 –

+1

@Vinny 직원이 사전에 저장됩니다. – notbob1

답변

0

코드에 몇 개의 print 문을 추가했습니다. 처음 두 요소가 처음에 추가되었고 세 번째 요소는 메뉴를 사용하여 추가되었습니다. , 0x037FFDB0에서 < 메인 .Employee 오브젝트> '2':

{ '1': 이 잉크로부터 출력 0x037FFE30에서 < 메인 .Employee 오브젝트> 3 : < 메인 .Employee object at 0x037FFA30>}

처음 두 개의 키는 문자열이고, 세 번째 키는 정수입니다.

당신이 사전에 정수 키를 추가하는 코드 (행 45, 55)를 수정하는 경우

는 :

empDatabase[int(newID)] = Employee(newName, newID, newDept, newTitle) 
empDatabase[int(newID2)] = Employee(newName2, newID2, newDept2, newTitle2) 

잘 작동하는 것 같다.

+0

방금 ​​ID를 요구하는 문 두 개에 eval을 추가 했으므로 완벽하게 작동합니다. 누락 된 기능 2 개가 세상의 차이를 만들 것이라고 누가 생각했을까요? – notbob1

+0

네가 내 대답을 올린 후에 미안하다. 어쨌든 도움이 되었기를 바라지 만 디버깅 연습으로 나에게 좋았다. – AlePorro

+1

문자열을 정수로 변환하는 올바른 방법은'eval()'이 아니라'int()'를 사용하는 것입니다. 'eval()'은 실행 가능한 코드를 받아들이는 반면'int()'는 입력이 유효한 정수가 아닌 경우 에러를 발생시킵니다. –

관련 문제