I have a simple piece of code that is not working and I can't begin to figure out why.
The code is as follows:
def myFunction(otherDictionary, mongoDbCollection):
for p in otherDictionary:
print('hi')
for d in mongoDbCollection:
print('hello')
Obviously the final goal isn't printing a bunch of hi's and hello's but did this purely for debugging purposes when it seemed like the looping mechanism was not functioning properly.
When I call this function to my dismay, a single hi is printed, then all of the hellos, followed by the rest of the his. Or something like this:
hi
hello
hello
hello
hello
hi
hi
hi
hi
rather than:
hi
hello
hello
hello
hello
hi
hello
hello
hello
hello
and so on.....
It definitely has something to do with the function's inputs as when I changed otherDictionary & mongoDbCollection each to [1,2,3,4,5] to debug this issue it printed the hi's and hello's as expected.
What could possible be in the inputs that would cause such an issue?
mongoDbCollection = a collection from my mongo database
otherDictionary is just a simple dictionary with keywords and respective counts for each like this:
{ 'randomKey': 10, 'otherRandomKey': 3, 'evenMoreRandomKey': 14 }
Could weird characters/symbols in the key's be causing an error like this?
I am completely stumped! The code is too simple for it not to be working...
mongoDbCollectionobject?for d...until it has first executedprint('hi'). So there is something else going on that isn't shown in your question. I suggest running your code under a Python debugger and single-stepping through it to gain some insight into what it is actually doing. What OS and code editor do you use, and what version of Python?