[Tutor] enumerate over Dictionaries

Alan Gauld alan.gauld at yahoo.co.uk
Sat Jul 6 04:10:34 EDT 2019


On 06/07/2019 05:28, Suhit Kumar wrote:

> I have made the complete program but when I am compiling the program it is
> showing errors. Can you please help to resolve this?
> The code is in the file attached with this mail.

And where are the errors?
Do not expect us to run unknown code received over the internet.
Only a fool would do such a thing!

Show us the error messages, with full traceback text.

A quick scan of the code reveals some stylistic things
that you could do but they are not the cause of your errors,
whatever they may be:


for i,ex in teacher.iterrows():
    lat1 = ex['Latitude']
    lon1 = ex['Longitude']
    Id = i

    for b,c in df.iterrows():
        lat2 = c['latitude']
        lon2 = c['longitude']
        nameVen = c['name']
        listEmpty.append((distanceCalculator(float(lat1),
                          float(lon1),float(lat2),float(lon2)),Id,b))

    demian = []
    listEmpty.sort()
    demian = listEmpty[0]
    dictionaryTeacher[ex['Name']] = demian
    listEmpty = []
    demian = []

listEmpty is a terrible name choice. You should never name variables
after their data type name them for the purpose they serve. And don;t
call it empty since that only applies at one particular poit. name it
after what you intend to store in it...

secondly you initialise demian to a list. Then you throw that list
away and assign a different value to it. The initial assignment is
pointless.

You initialise listEmpty outside the loop and then again at the end.
If you move the initialisation into the loop body at the top you
won't need to reset it at the end. It only saves a line of code
but if you ever need to change the initial value it means you only
need to do it in one place.

I don't have time to read through the rest. You really should
refactor your code into functions. It will make it easier
to modify, easier to debug, and much easier to read and discuss.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list