Loops In a Row | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Loops In a Row

I noticed that when I use for loop on top of each other, the bottom one has no effect. What is the reason of this? https://code.sololearn.com/c9D75rQM2zPk/?ref=app

1st Oct 2020, 1:21 PM
Nadirhan Şahin
Nadirhan Şahin - avatar
5 Answers
+ 2
You are using generator's yield function, which is don't save values after function calling over until it called again.. So there in loop, the value for c in none in last loop. Check this def count(): for i in range (1,6): yield(i) c=count() print(*c) #output 1,2,3,4 5 print(*c) #output nothing.. So 2 loop not runs. In your code that why only one loop running..
1st Oct 2020, 1:52 PM
Jayakrishna 🇮🇳
+ 5
the function you are using is a generator function. this will be stored in varibale c. Then you iterate over c. This can only be done ones, because then the generator is exhausted after iteration is fished. if you need to iterate twice, you should rethink your code / algorithm, or you have to run the generator function again. The reason of this is, that the generator does not create all the values when the function is called and then return them. using yield gives only one value back at a given iteration cycle.
1st Oct 2020, 1:57 PM
Lothar
Lothar - avatar
+ 2
Can you show the code?
1st Oct 2020, 1:23 PM
Jayakrishna 🇮🇳
1st Oct 2020, 1:36 PM
Nadirhan Şahin
Nadirhan Şahin - avatar
0
Jayakrishna🇮🇳 thanks i understand
1st Oct 2020, 1:55 PM
Nadirhan Şahin
Nadirhan Şahin - avatar