Problem with 'purify'


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/2?curriculum_id=4f89dab3d788890003000096#


Error code: "Oops, try again. Does your purify function take exactly one argument (a list of numbers)? Your code threw a "'int' object is not iterable" error."


I expected this to work...


def purify(x):
    for i in x:
        numbers = []
        if i%2 == 0:
            numbers.extend(i)
    return numbers

https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/2?curriculum_id=4f89dab3d788890003000096#


#2

It is important to do all initialization before the loop.

numbers = []
for ...

#3

Thanks, but I get the same error.....:frowning:


#4

Usage of the extend method is slightly different from append.

>>> def purify(x):
	numbers = []
	for i in x:
		if i % 2 == 0:
			numbers.extend(i)
	return numbers

>>> purify([1,3,5,7,2,4,6,8])
Traceback (most recent call last):
  File "<pyshell#175>", line 1, in <module>
    purify([1,3,5,7,2,4,6,8])
  File "<pyshell#174>", line 5, in purify
    numbers.extend(i)
TypeError: 'int' object is not iterable
>>>

The method is intended for extension of a list with multiple items, also in a list.

>>> def purify(x):
	numbers = []
	for i in x:
		if i % 2 == 0:
			numbers.extend([i])
	return numbers

>>> purify([1,3,5,7,2,4,6,8])
[2, 4, 6, 8]

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.