Assume I have a file of the following format:
a,1
b,2
c,3
d,4
Here is my code:

def junk(f):
    d1 = {}
    d2 = {}
    for line in f:
        columns = line.split(",")
        letters = columns[0]
        numbers = columns[1]
        d1[letters] = numbers
        d2[numbers] = letters
    return (d1, d2)
    
def something():  
    print d1
    print d2
    
if __name__ == "__main__":
    f = open("filename.txt")
    d1 = junk(f)[0]
    d2 = junk(f)[1]

Assume I want to call function something. It cannot print d1 and d2 unless I add them to the main block. As soon as I add d1 and d2 to the "main" block and call either of the two functions, both return two empty dictionaries. Why does this happen? How can I fix it? Please help! I've been trying to figure it out the whole day!

Recommended Answers

All 3 Replies

What is the load_airports function? Are you sure that it is returning something into your dictionaries?

What is the load_airports function? Are you sure that it is returning something into your dictionaries?

Sorry, copied a part of the other program. I fixed it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.