Your generator works. You forgot to iterate over it though:
for elem in generator(100):
print elem
or you could turn it into a list:
print list(generator(100))
You instead printed the generator object produced by calling the generator function. A generator function produces a suspended generator. Only when you iterate over it is code executed (until the next yield
).
Demo:
>>> def generator(n):
... list_of = range(1,n+1)
... for i in list_of:
... if i % 7 == 0:
... yield i
...
>>> print list(generator(100))
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
The list()
call iterates over the given argument, producing a Python list object with all elements yielded by the argument. Great for iterating over a generator to pull in all the elements that it produces.
As for KOD; that editor hasn't seen updates in years now; you may want to switch to something else. As the KOD twitter feed stated 2 years ago:
Don't wait for me, I'm kind of like a Zombie. Go get Sublime Text @sublimehq which is awesome: http://www.sublimetext.com
I concur; Sublime Text is my current editor of choice.