does anyone know why this code doesnt work? still infinite | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

does anyone know why this code doesnt work? still infinite

https://code.sololearn.com/cAPd021Il7b8/?ref=app

5th Oct 2017, 12:52 AM
Aakash Bansal
Aakash Bansal - avatar
8 Answers
+ 1
You can get out that way. It's just that on line 11, there's no condition - it always adds 1. # So under what condition should... count+=1
5th Oct 2017, 1:19 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Replace line 8: print(i) with this: if count > 50: break print("i({}) == s({}) and count({}) == n({})".format(i, s, count, n)) It appears you miss the target at output line 5. [edited simultaneously with answer] ------------------------------------- I assume you know that cycle() is infinite, which is why you're using return to get out... but just in case my interpretation isn't fun: ------------------------------------- print(help(cycle)); quit() cycle .... Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.
5th Oct 2017, 1:06 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
um im not trying to print the if condition thats when I want the cycle print to break....
5th Oct 2017, 1:10 AM
Aakash Bansal
Aakash Bansal - avatar
+ 1
The only way you're going to get out is if this condition is satisfied at lines 9-10: if i==s and count==n: return Checking your condition is just part of debugging; I added the break so I had exit control. You don't have to use any of that -- I just wanted you to see the condition "miss" the target.
5th Oct 2017, 1:12 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
ah so cycle cannot be returned out of? I want the print to specifically end at specified iteration such as 4th time the number 5 appears in the 1-9 cycle count...
5th Oct 2017, 1:14 AM
Aakash Bansal
Aakash Bansal - avatar
+ 1
Ah i get it! it was adding 1 at every cycle step instead of complete cycle. I initially meant to do it with the count itertools function but couldnt figure out how to run both in parellel
5th Oct 2017, 1:23 AM
Aakash Bansal
Aakash Bansal - avatar
+ 1
I fixed it with adding if i==l[-1]: as a condition to the count! thanks alot for the assist!
5th Oct 2017, 1:27 AM
Aakash Bansal
Aakash Bansal - avatar
+ 1
That's right. Thanks for explaining too; that helped me get closer to a better hint.
5th Oct 2017, 1:27 AM
Kirk Schafer
Kirk Schafer - avatar