This is a snip-it of python I am working on right now – I did not list all of this code, so I apologize if something you need is “missing” – I think I can explain it well enough without the rest of it…
Below there is a function main() – this is not explicitly defined in my script – it is imported from another script made by someone else. When it is called, it outputs a very long list of every single available module python has available to call. I am trying to add line numbers to each module. So when it outputs it’s a very long list of module names (I assume the function main() is putting “\n” breaks after each module because it prints one module, then a new line, then another module name). What I am TRYING to do is take those values, and add a line number in front of each module name.
elif x == "list" or x == "1":
print "\n loading... please wait"
time.sleep(2)
counter=0
lnumber = 0
all_mods = (main())
for x in all_mods:
print lnumber, x
lnumber = lnumber + 1
counter = counter + 1
print "-" * 30, "\nTotal number of modules detected: ", counter
**I understand the lnumber and counter are reporting the same thing, however I did this on purpose because it is consistent with the code I have elsewhere in the document which did not use this setup.
When this snippet of code is ran (with the other parts of the script) it reports back:
Traceback (most recent call last):
File "C:\Users\jc\Documents\Python Projects\Projects\myOwnfns\helpwiz.py", line 131, in <module>
main_loop()
File "C:\Users\jc\Documents\Python Projects\Projects\myOwnfns\helpwiz.py", line 90, in main_loop
for x in all_mods: #this variable comes from "list_all_mods" - an external script taken from another author.
TypeError: 'NoneType' object is not iterable
The function
mainprints the lines to standard output; it doesn’t return anything. More precisely it returns theNoneobject, soall_mods is None. That’s the cause for “‘NoneType’ object is not iterable”, because you’re trying to iterate over it withfor x in all_mods.Here’s a terribly hackish solution that will work: