0

I have some code for car registration for parking. I have created a dictionary with car registration number as keys and rest information as values. I am trying to get details (values) of each registration by entering the registration number. Even if the id is in the dictionary it's showing message not found in the dictionary.

global variable data_dict = {} def createNameDict(filename): path = "C:\Users\user\Desktop" basename = "ParkingData_Part2.txt" filename = path + "\\" + basename file = open(filename) contents = file.read() print contents,"\n" data_list = [lines.split(",",1) for lines in contents.split("\n")] #data_list.sort() #print data_list #dict_list = [] for line in data_list: keys = line[0] values = line[1] data_dict[keys] = values print data_dict,"\n" print data_dict.keys(),"\n" print data_dict.values(),"\n" print data_list def detailForRegistrationNumber(regNumber): regNumber == "keys" if regNumber in data_dict: print data_dict[regNumber] else: print regNumber, "Not in dictionary" 

The error message I am getting is:

======= Loading Progam ======= >>> detailForRegistrationNumber('EDF768') EDF768 Not in dictionary 

But the dictionary has the above registration number:

{'HUY768': ' Wilbur Matty, 8912, Creche_Parking', 'GH7682': ' Clara Hill, 7689, AgHort_Parking', 'GEF123': ' Jill Black, 3456, Creche_Parking', 'WER546': ' Olga Grey, 9898, Creche_Parking', 'TY5678': ' Jane Miller, 8987, AgHort_Parking', 'ABC234': ' Fred Greenside, 2345, AgHort_Parking', 'KLOI98': ' Martha Miller, 4563, Vet_Parking', **'EDF768'**: ' Bill Meyer, 2456, Vet_Parking', 'JU9807': ' Jacky Blair, 7867, Vet_Parking', 'DF7800': ' Jacko Frizzle, 4532, Creche_Parking', 'ADF645': ' Cloe Freckle, 6789, Vet_Parking'} 
3
  • If you use backslashes in your paths (protip: don't, even windows supports forward slashes!), you need to escape them or simply use a raw string: r'C:\whatever\path' Commented May 13, 2012 at 14:05
  • Where is your 'main' method? Where does createNameDict get called? From the code snippet above, I can't tell if it gets called or you forgot to call it. If you forgot to call it, then calling detailForRegistrationNumber for any argument will say it's not in the dictionary. Commented May 13, 2012 at 14:07
  • 3
    you don't appear to be following any advice from your other post regarding how to input that text: stackoverflow.com/questions/10545544/… Commented May 13, 2012 at 14:08

1 Answer 1

2

I think your problem is that your function def createNameDict(filename): doesn't return anything, so the data_dict inside it is just a local variable!

Make the last line of the function return data_dict and then use it like data_dict = createNameDict(filename). There is no need for the global variable part, so just remove that.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.