How to solve error: Zip argument #1 must support iteration

27,199

Solution 1

Your gen_num_pointers() and gen_num_words() methods return an integer. zip() can only work with sequences (lists, sets, tuples, strings, iterators, etc.)

You don't need to call zip() at all here; you are testing one integer against another:

def test_get_num_words(self):
    word_part = ['13797906', '23', 'n', '04', 'flood', '0', 'inundation', '0', 'deluge', '0', 'torrent', '0', '005', '@', '13796604', 'n', '0000', '+', '00603894', 'a', '0401', '+', '00753137', 'v', '0302', '+', '01527311', 'v', '0203', '+', '02361703', 'v', '0101', '|', 'an', 'overwhelming', 'number', 'or', 'amount;', '"a', 'flood', 'of', 'requests";', '"a', 'torrent', 'of', 'abuse"']
    self.assertEqual(4, self.wn.get_num_words(word_part))

def test_get_num_pointers(self):
    before_at = '13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005'
    self.assertEqual(5, self.wn.get_num_pointers(before_at))

is plenty.

You also want to avoid using a leading 0 on integer literals. 04 is interpreted as an octal number; if you ever had to change that number to using more digits, or using digits outside of the range 0-7, you'd be in for a nasty surprise:

>>> 010
8
>>> 08
  File "<stdin>", line 1
    08
     ^
SyntaxError: invalid token

Solution 2

Your test should look like this:

def test_get_num_pointers(self):
    before_at = '13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005'
    expected = 5
    real = self.wn.get_num_pointers(before_at)
    self.assertEqual(real, expected)

It only makes sense to use a for loop when you're asserting more than one value.

Solution 3

The error explains what is going on: the arguments to zip need to be iterables (ie lists, tuples, or something else you can actually iterate through). You are passing ints, ie single numbers.

I am not sure exactly what you are trying to do by calling zip, but maybe you just want to compare real and expected directly?

Share:
27,199
JohnConneely
Author by

JohnConneely

Updated on July 03, 2020

Comments

  • JohnConneely
    JohnConneely almost 4 years

    I have two test methods with the same problem, here are the original methods in the main class:

    def get_num_words(self, word_part):
        """ 1 as default, may want 0 as an invalid case """
        if word_part[3] == '0a':
            self.num_words = 10
        else:
            self.num_words = int(word_part[3])
        return self.num_words
    
    def get_num_pointers(self, before_at):
        self.num_pointers = int(before_at.split()[-1])
        return self.num_pointers
    

    And here are the two test classes:

    def test_get_num_words(self):
        word_part = ['13797906', '23', 'n', '04', 'flood', '0', 'inundation', '0', 'deluge', '0', 'torrent', '0', '005', '@', '13796604', 'n', '0000', '+', '00603894', 'a', '0401', '+', '00753137', 'v', '0302', '+', '01527311', 'v', '0203', '+', '02361703', 'v', '0101', '|', 'an', 'overwhelming', 'number', 'or', 'amount;', '"a', 'flood', 'of', 'requests";', '"a', 'torrent', 'of', 'abuse"']
        expected = 04
        real = self.wn.get_num_words(word_part)
        for r, a in zip(real, expected):
            self.assertEqual(r, a)
    
    def test_get_num_pointers(self):
        before_at = '13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005'
        expected = 5
        real = self.wn.get_num_pointers(before_at)
        for r, a in zip(real, expected):
            self.assertEqual(r, a)
    

    This is the error they are giving out: TypeError: zip argument #1 must support iteration The program works fully and these are the only 2 tests not working in 20 different tests.