+ 1
does anyone know why this code doesnt work? still infinite
8 Respuestas
+ 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
+ 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.
+ 1
um im not trying to print the if condition thats when I want the cycle print to break....
+ 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.
+ 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...
+ 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
+ 1
I fixed it with adding if i==l[-1]: as a condition to the count! thanks alot for the assist!
+ 1
That's right. Thanks for explaining too; that helped me get closer to a better hint.