In: Computer Science
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong?
from Lab_6.Employee import *
def file_to_directory(File):
myDirectory={}
with open(File,'r') as f:
data=f.read().split('\n')
x=(len(data))
myDirectory = {}
for line in range(0,199):
myDir=data[line].split(",")
firstName=myDir[0]
lastName=myDir[1]
title=myDir[2]
email=myDir[3]
phone=myDir[4]
office=myDir[5]
dept=myDir[6]
category=myDir[7]
myDir=[firstName, lastName, title, email, phone, office, dept, category]
employ=Employee()
Employee.setFirstName(employ, firstName)
Employee.setLastName(employ, lastName)
Employee.setPhone(employ, phone)
Employee.setTitle(employ, title)
Employee.setEmail(employ, email)
Employee.setOffice(employ, office)
Employee.setDept(employ, dept)
Employee.setCategory(employ, category)
#print (Employee.__str__(employ))
myDirectory[phone[-4:]]=myDir
#print (myDirectory)
return myDirectory
def firstpart(myDirectory):
inn=input("Enter the number for what you want. \n1. display entire directory\n2. look up by extension\n3.Remove an employee\n4.Add an employee\n5.Exit")
if inn=="1":
print (myDirectory)
elif inn=="2":
a = input ("Enter the extension number please.")
print (myDirectory[a])
elif inn =="3":
q=input ("please enter a phone number you would like to remove.")
del myDirectory[q]
print (myDirectory)
elif inn=="4":
z=input ("Please enter a phone number.")
myDirectory = myDirectory + {'phone':myDirectory[z] }
elif input=="5":
print ("you have now exited the program.")
dictionary = file_to_directory((f"../data/directory.txt"))
firstpart(dictionary)
dictionary in python stores key,value pair. So it assumes key to be unique for each insertion.
According to the given code,
So,in def file_to_directory(File), myDictionary is a dictionary. Here you are using myDirectory[phone[-4:]]=myDir to store the myDir which is a list of employee data. So myDictionary is assuming the last 4 digits of the phone number as a key. For Example: If the phone number is 9091190911, then myDictionary['0911']=myDir.
I suggest you to use phone as a key. not phone[-4:], using so will make a unique key to each employee. There may be a case where 2 persons have the same last 4 digits of a phone number. So better don't use that.
Coming to def firstpart(myDirectory):, here in menu 4, you basically have to add an employee with new phone number(Note: Does this mean adding the employee to the dictionary with phone number as a key? or updating the existing employee with a new phone number? The clarity is missing here. PLEASE clarify this in the comments section.). I assume, adding the employee to the dictionary with phone number as a key. So follow the code below.
elif inn=="4":
z=input ("Please enter a phone number.")
if z not in myDirectory.keys():
#if phone not in myDirectory, then add it to the dictionary by entering the fname,lname,title etc..
myDir=[firstName, lastName, title, email, phone, office, dept, category] #please input all these data here
myDirectory[z]=myDir #this will add the employee(myDir) with new phone number as z.
Hope you understood the solution and where you are doing wrong. Feel free to ask doubts if any, in the comments section.
#Please don't forget to upvote if you find the solution helpful. Thank you.