[英]Dictionary lookup won't recognize initial input
Hello StackOverflow community, I am very close to finishing the creation of a employee database program which implements a dictionary for storage.你好 StackOverflow 社区,我非常接近完成一个员工数据库程序的创建,该程序实现了一个存储字典。 The issue I have been running into is that the initial Employee objects that are stored before the menu prompt are unable to be located.
我遇到的问题是无法找到在菜单提示之前存储的初始 Employee 对象。 They exist in the database, but I cannot manipulate them at all with my code.
它们存在于数据库中,但我根本无法用我的代码操作它们。 Anything I add afterwards using the menu functions is able to be looked up or deleted.
我之后使用菜单功能添加的任何内容都可以被查找或删除。 I'm not really sure what the issue is here.
我不太确定这里的问题是什么。 I tried making the dictionary global to no avail.
我尝试将字典设为全局,但无济于事。 The statements I use are nearly identical.
我使用的语句几乎相同。
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()
I added a couple of print statements along the code.我在代码中添加了几个打印语句。 The first two elements were added initially, the third one was added using the menus.
最初添加了前两个元素,使用菜单添加了第三个元素。 This is the output from the print:
这是打印的输出:
{'1': < main .Employee object at 0x037FFDB0>, '2': < main .Employee object at 0x037FFE30>, 3: < main .Employee object at 0x037FFA30>} {'1':<位于 0x037FFDB0 处的主.Employee 对象>,'2':<位于 0x037FFE30 处的主.Employee 对象>,3:<位于 0x037FFA30 处的主.Employee 对象>}
As you can see the first two keys are string, the third one an integer.如您所见,前两个键是字符串,第三个是整数。
If you modify your code (rows 45 and 55) to add integers keys to the dictionary:如果您修改代码(第 45 行和第 55 行)以将整数键添加到字典中:
empDatabase[int(newID)] = Employee(newName, newID, newDept, newTitle)
empDatabase[int(newID2)] = Employee(newName2, newID2, newDept2, newTitle2)
it seems to work fine.它似乎工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.