python - Using "yield" in a function -
i want generate in function receives 1 argument n using yield generate:
1 1+2 1+2+3 … … 1+2+3+⋯+n−1+n that last try:
def suite(n): total = 0 in n: total+=i yield total and receive:
traceback (most recent call last): file "notebook", line 4, in suite typeerror: 'int' object not iterable
your error here:
for in n: n integer , not iterable. perhaps wanted use xrange() (python 2 only) or range() (recommended on python 3) here:
for in range(n): note starts iteration @ 0, not 1 (up , including n - 1). either use range(1, n + 1), or add 1 sum:
def suite(n): total = 0 in range(n): total += + 1 yield total this hasn't got generators; wether or not used yield, trying loop on plain int object doesn't work either way.
Comments
Post a Comment