Course 5 Week 3 Assignment 2, Error in exercise-2

Hi, after running the insert_audio_clips function, it passes all the tests, but executing the cell right after the unit tests gives the following error

RecursionError                            Traceback (most recent call last)
<ipython-input-23-7ea09631b600> in <module>
      1 np.random.seed(5)
----> 2 audio_clip, segment_time = insert_audio_clip(backgrounds[0], activates[0], [(3790, 4400)])
      3 audio_clip.export("insert_test.wav", format="wav")
      4 print("Segment Time: ", segment_time)
      5 IPython.display.Audio("insert_test.wav")

<ipython-input-21-d222fe5d1b11> in insert_audio_clip(background, audio_clip, previous_segments)
     35     # if last try is not overlapping, insert it to the background
     36     if not is_overlapping(segment_time, previous_segments):
---> 37         insert_audio_clip(background, audio_clip, previous_segments)
     38     ### START CODE HERE ###
     39         # Step 3: Append the new segment_time to the list of previous_segments (≈ 1 line)

... last 1 frames repeated, from the frame below ...

<ipython-input-21-d222fe5d1b11> in insert_audio_clip(background, audio_clip, previous_segments)
     35     # if last try is not overlapping, insert it to the background
     36     if not is_overlapping(segment_time, previous_segments):
---> 37         insert_audio_clip(background, audio_clip, previous_segments)
     38     ### START CODE HERE ###
     39         # Step 3: Append the new segment_time to the list of previous_segments (≈ 1 line)

RecursionError: maximum recursion depth exceeded while calling a Python object

All the previous exercises had passed all the tests as well but I’m getting this error for some reason.

1 Like

At line marked 37, your code in insert_audio_clip() is calling itself. Code that calls itself is a “recursion”. That’s generally a bad thing, because it can lead to an infinite loop.

What you should do there is append the segment time to the list of previous segments.

2 Likes

But, I hadn’t modified that part of the code as it didn’t come under the ### START CODE HERE ### section. Also I had appended the segment time in step 3 (right after the recursive call) as was indicated.

There are two blocks of code you modify in this function.

Your code won’t run after the recursive call, because the code keeps calling itself. It never gets to the next line.

I did modify both the blocks and the code passes all the unit tests. I just get this error while running this block :-----

np.random.seed(5)
audio_clip, segment_time = insert_audio_clip(backgrounds[0], activates[0], [(3790, 4400)])
audio_clip.export("insert_test.wav", format="wav")
print("Segment Time: ", segment_time)
IPython.display.Audio("insert_test.wav")

Did you fix the recursion issue I that I mentioned?

1 Like

Thanks! Works after restarting the kernels!