[W3 A2] Ex 4 - create_training_example - TypeError: cannot unpack non-iterable int object

in the create_training_example() function I’m getting the error:
TypeError: cannot unpack non-iterable int object

Here is the complete console output:
TypeError Traceback (most recent call last)
16 print("\033[92m All tests passed!")
—> 18 create_training_example_test(create_training_example)

<ipython-input-35-cfc8a2ca224c> in create_training_example_test(target)
      2 def create_training_example_test(target):
      3     np.random.seed(18)
----> 4     x, y = target(backgrounds[0], activates, negatives, 1375)
      6     assert type(x) == np.ndarray, "Wrong type for x"

<ipython-input-34-429cc8379286> in create_training_example(background, activates, negatives, Ty)
     37     for random_activate in random_activates: # @KEEP
     38         # Insert the audio clip on the background
---> 39         background, segment_time = insert_audio_clip(background, random_activate, previous_segments)
     40         # Retrieve segment_start and segment_end from segment_time
     41         segment_start, segment_end = segment_time

<ipython-input-15-2d2eb92a43e2> in insert_audio_clip(background, audio_clip, previous_segments)
     28     # we retry 5 times(≈ 2 lines)
     29     retry = 5 # @KEEP
---> 30     while (is_overlapping(segment_time, previous_segments) and retry >= 0):
     31         segment_time = get_random_time_segment(segment_ms)
     32         retry = retry - 1

<ipython-input-27-24308bd72e3d> in is_overlapping(segment_time, previous_segments)
     22         # Step 2: loop over the previous_segments start and end times.
     23         # Compare start/end times and set the flag to True if there is an overlap (≈ 3 lines)
---> 24         for previous_start, previous_end in previous_segments: # @KEEP
     25             if segment_start <= previous_end and segment_end >= previous_start:
     26                 overlap = True

TypeError: cannot unpack non-iterable int object

According to this line:
---> 24 for previous_start, previous_end in previous_segments: # @KEEP

the problem seems to be the list previous_segments that cannot be unpacked to the 2 values previous_start, previous_end

But the thing is that we initialize previous_segments as an empty list in the function, and then we append the segment_time (containing 2 values) inside the insert_audio_clip() function so there shouldn’t be an issue.

Unfortunately, as there is no visual debugger in the Jupyter notebook, I can’t really see what is being past in previous_segments to the function.

Do you have any recommendation for this issue?
Please let me know if you need the Notebook to check it in further details.

Many thanks for your help!!

Does your is_overlapping() function pass all of its tests? Specifically it should return False if previous_segments is null.

Did you initialize “overlap” correctly (it’s Step 1 for that function) ?

I encountered this error as well, but figured out that it’s because of a coding error in the insert_audio_clip() function.
Maybe worth checking if you are appending the correct element to the previous_segments list. I made the mistake there.


I used previous_segments.extend(list(segment_time)) in insert_audio_clip() function, which passed all the test perfectly fine, but I got the error you got. I changed this part with previous_segments.append(segment_time) and everything works fine.