Python 查字典赢了';无法识别初始输入
大家好,我的社区, 我即将完成一个员工数据库程序的创建,该程序实现了一个用于存储的字典。我遇到的问题是,无法找到在菜单提示之前存储的初始Employee对象。它们存在于数据库中,但我根本无法用代码操纵它们。我以后使用菜单功能添加的任何内容都可以查找或删除。我真的不确定这里有什么问题。我试图使这本词典全球化,但没有用。我使用的语句几乎相同Python 查字典赢了';无法识别初始输入,python,class,object,dictionary,instance,Python,Class,Object,Dictionary,Instance,大家好,我的社区, 我即将完成一个员工数据库程序的创建,该程序实现了一个用于存储的字典。我遇到的问题是,无法找到在菜单提示之前存储的初始Employee对象。它们存在于数据库中,但我根本无法用代码操纵它们。我以后使用菜单功能添加的任何内容都可以查找或删除。我真的不确定这里有什么问题。我试图使这本词典全球化,但没有用。我使用的语句几乎相同 class Employee: def __init__(self, name, idNo, department, title): s
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()
我在代码中添加了几个print语句。 最初添加了前两个元素,使用菜单添加了第三个元素。 这是打印输出: {'1':,'2':,3:} 正如您所看到的,前两个键是字符串,第三个键是整数 如果修改代码(第45行和第55行)以将整数键添加到字典中:
empDatabase[int(newID)] = Employee(newName, newID, newDept, newTitle)
empDatabase[int(newID2)] = Employee(newName2, newID2, newDept2, newTitle2)
它似乎工作正常。你在哪里加载现有员工?@Vinny员工存储在字典EMPDatabase中我真的无法强调你应该避免
eval
,除非你真的需要它,而且你肯定不需要它。有关详细信息,请参见SO资深内德·巴奇尔德。当然,这可能只是一个“玩具”程序,不适合在现实世界中使用,因此您不必担心有人会输入一个命令来擦除您的硬盘,而不是一个有效的ID号。但是对琐碎的东西使用eval
绝对不是一个好的做法。将字符串转换为整数的正确方法是使用int()
,而不是eval()
。另请参见示例。你不能高估它的威胁。我刚刚在两份要求身份证的声明中添加了eval,它工作得非常好。谁会想到两个缺失的函数会有很大的不同?是的,很抱歉,我只是在发布我的答案后才注意到你的评论。希望它能帮上忙,但作为调试的练习,它对我有好处!将字符串转换为整数的正确方法是使用int()
,而不是eval()
eval()
将接受任何可执行代码,而int()
将在输入不是有效整数时抛出错误。