I am trying to find the number of zero crossing in a list. The code I am using is:
for i in range(1, len(value)): zerocrossing = 0 if ((value[i-1]) > 0 and value[i] < 0): zerocrossing += 1 if ((value[i-1]) < 0 and value[i] > 0): zerocrossing += 1 stdio.writeln('The Number of Zero Crossings is ' + str(zerocrossing))
This code doesn’t give me any errors, but it’s not giving me the right answer. If I give it the input [1.0, -1.0, 1.0]
it gives me 1
, but it should give 2
. What am I doing wrong?
Advertisement
Answer
You’re setting zerocrossing
to zero at every loop iteration. Move zerocrossing = 0
outside the for-loop.