printing - Python For loop multiple returns -
i supposed write function 1 of computer science classes in python. function supposed take in startvalue , increment until numberofvalues reached. function far:
def nextnvalues(startvalue, increment, numberofvalues): result = int(0) in range(0, numberofvalues): increase = * increment result = startvalue + increase return result
i call doing:
print(nextnvalues(5,4,3))
the problem output 13. how make returns number each time increments. example, 5, 9, 13? have been having problem previous functions have been adding , removing things without logic work. doing wrong?
this perfect use case generators.
long story short, use yield
instead of return
:
def nextnvalues(startvalue, increment, numberofvalues): result = int(0) in range(0, numberofvalues): increase = * increment result = startvalue + increase yield result
the clients of code can use either in simple loop:
for value in nextnvalues(...): print(value)
or can list if needed converting list
. example, if 1 needed print result:
print(list(nextnvalues(...)))
Comments
Post a Comment