Issue
I have written a "for" "in" "range" loop in python, and placed a "break" function for the loop to end at a specific condition. I need to produce a count for how many iterations the loop makes before it stops at the break point, but I can't find the right function. Any ideas?
I have attempted to create a list of the the range() values, and then use the len() function to get a count, but the range() list doesn't recognize the break function, and it just gives me a list of every iteration until the range ends.
for val in range(Nmax):
x_n1 = x_n - f(x_n)/fprime(x_n)
print(x_n1)
if( abs((x_n1 - x_n) / (0.5 * (x_n1 +x_n))) < delta ):
break
x_n = x_n1
The output is a list of numbers for each successful iteration. I need to produce an additional output that provides a count of all of the outputs before the break is hit.
Solution
Since you are using range
, val
will count loop iterations.
for val in range(Nmax):
x_n1 = x_n - f(x_n)/fprime(x_n)
print(x_n1)
if( abs((x_n1 - x_n) / (0.5 * (x_n1 +x_n))) < delta ):
break
x_n = x_n1
print(val)
Answered By - Deepstop
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.