Purify - String string prints [u'String']


#1

Getting "not all arguments converted during string formatting" print statement shows [u'1', u'2', u'3', u'4', u'5'] . I have no idea of what I'm doing wrong...

def purify(numbers):
    pure = []
    numbers = list(numbers)
    print numbers
    for l in numbers:
        if l%2 == 0:
            pure = pure.append[l]
            print pure
        else:
            continue
    return pure
    
print purify(raw_input("Your list"))

#2

OK, I added

str(raw_input("Your list"))

and it removed the 'u', but I'm still getting the error below:

Traceback (most recent call last):
File "python", line 13, in
File "python", line 6, in purify
TypeError: not all arguments converted during string formatting


#3

@textpro62836 ,

You have this ...

pure = pure.append[l]

What does the append method return? You can Google it to find out.


#4

Additionally, the function isn't meant to take a string as input


#5

Consistent with @ionatan 's comment, convert the user's input to a list of ints before passing it to the purify function. One way to do it would be ...

# get user input as a str
inp = raw_input("Enter some integers separated by spaces: ")
# list comprehension to convert inp to list of numbers
lst = [int(i) for i in inp.split()] 
# pass the list to purify and display result
print purify(lst)

If you wish to take this approach, Google Python list comprehensions.


#6

OK, so i changed it to look like this (got a bunch of print statements for debug):

numbers = raw_input("Your list")

def purify(numbers):
    pure = []
    numb = [int(i) for i in numbers.split()]
    print numb
    for l in numb:
        if l%2 == 0:
            pure.append(l)
            print pure
        else:
            continue
    return pure


print purify(numbers)

It gives me the right answer, but it also complains about "'list' object has no attribute 'split'" error. When did 'numbers' become a list? Shouldn't it be a string?

Your list 1 2 3 4 5
[1, 2, 3, 4, 5]
[2]
[2, 4]
[2, 4]
None


#7

No, this should not be done inside the function ...

numb = [int(i) for i in numbers.split()]

Codecademy wants you to pass a list of ints to the purify function. That is the basis of @ionatan 's comment.

Perform the conversion of the user's input to a list of ints before calling the function.


#8

Alright, i'm feeling pretty dumb right now... Now i'm getting 'TypeError: not all arguments converted during string formatting'

numbers = raw_input("Your list")
numb = [int(i) for i in numbers.split()]
print numb

def purify(numb):
    pure = []
    for l in numb:
        if l%2 == 0:
            pure.append(l)
            print pure
        else:
            continue
    return pure


print purify(numbers)

#9

You have this ...

print purify(numbers)

What type of object is numbers, which is what that statement is passing to the function?


#10

Well, that was dumb haha

so i forgot to change it to print purify(numb).

Looks like the one yesterday where i messed up += with =+ and spent 20 mins looking at the code.

Gotta pay more attention....

Thanks!


#11

Is it all working now?

Your purify function looks OK, but it does have a few unnecessary statements. For example, the else block can be removed because it does not really do anything.


#12

Yeah, all working now!

oh, i thought that for every if you need an else statement. thanks for the heads up!

While i'm here, the only way to change a str to int is via numb = [int(i) for i in numbers.split()] ?


#13

We used this because we needed to convert a str to a list of ints ...

lst = [int(i) for i in inp.split()]

For a str that can be converted to a single int, the procedure is much simpler. For example ...

num_str = raw_input("Enter an integer: ")
num = int(num_str) # This line converts to an int
print "%d squared is %d" % (num, num ** 2)

Console ...

Enter an integer:  7
7 squared is 49

#14

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