When I run the code I get the following output
How do I print the print the output?
def firstn(n):
num=0
while num < n:
yield num
num=num+1
sum_of_first_n=sum(firstn(10))
print(firstn(3))
In general:
print(list(firstn(n)))
Be sure your generator is not infinite. If you are not sure, use something like:
import itertools as it
print(list(it.islice(firstn(n), 100)))
to print up to the first 100 elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With