Help with challenge problem | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Help with challenge problem

https://code.sololearn.com/c4TZQ4FZlRUh/?ref=app I got this challenge problem wrong and Iā€™m really confused on how the answer is 5. Can you explain what for i in c means? If you substitute in the function, you would get for i in for i in range(1,6), which doesnā€™t make any sense to me. How many times is that function being looped through? Also, why isnā€™t the second for i in c function being looped through?

27th Jun 2022, 4:52 AM
Hubert Huang
1 Resposta
+ 1
The yield function returns a generator and generators can only be iterated over once. When you store count_to_five() in c, what youā€™re actually storing is a generator object. You iterate over this generator once in your first for loop but then the second for loop is not executed because you have already iterated over the generator. Thatā€™s the reason why the count results 5 instead of going back to zero. You can insert some prints in order to better visualize your codeā€™s behavior. Yield and generators are very well explained here: https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do
27th Jun 2022, 6:43 AM
Alvaro GV