16/18 Using String Lists in Functions


#1

Hey everyone!,
Normally I'm able to solve whatever issue I'm having by looking up other users threads but for this one I just can't seem to get anything to work. I've gone over it a ton of times and I keep getting
"Traceback (most recent call last):
File "python", line 10, in
File "python", line 6, in join_strings
TypeError: bad operand type for unary +: 'str'"

Below I have entered my code for this problem. If anyone can tell me why the module isn't accepting this I'd be eternally grateful. Thanks in advance!

n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
result = ""
for i in range(len(words)):
result =+ words[i]
return result

print join_strings(n)`

EDIT: Solved I had =+ instead of +=


#2

Well, your code seems alright. May be is your indentation that has a problem, likely your return result should only have four spaces or a single tap.
My code:
n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
result = ""
for me in range(len(words)):
result += words[me]
return result
print join_strings(n)indent preformatted text by 4 spaces


#3
n = ["Michael", "Lieberman"]

Add your function here

def join_strings(words):
    result = ""
    for me in range(len(words)):
        result += words[me]
    return result
print join_strings(n)indent preformatted text by 4 spaces

#4

A post was split to a new topic: Code doesn't pass


#5

As the OP says, he had to place += instead of +=. Post solved.


#6

ok first off while i figured out what you meant you have "+= instead of +=" and even after i swapped that i still get an error that says

"Oops, try again. join_strings(['x', 'y', 'z', 'a']) returned 3 instead of 'xyza'"


#7

Your code above does not generate this error. You must be using a different code.


#8

Here is a direct screen cap.


#9

First up, you do need to use the +=, it's the correct way.
result += i is just saying you're going to add i to result, and you haven't defined i
result += words[i] is saying you're adding the index from words (our parameter).

Hopefully that made sense


#10

Technically, i is defined. It takes values from 0 until len(words) (exclusive).


#11

My issue with this problem was that I used an undefined variable. I used a random variable (i) instead of the one that Python gave me (n). I am not 100% sure that this was my only problem but after fixing the variable, this code worked.


#12

what is wrong with mine?/??/??/???

n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    result = ""
    for n range(len(words)):
        result += words[n]
    return result

print join_strings(n)

#13

Hi sharklover_333,

for n range(len(words)):

Don't you think you are missing something in the above line? :wink:


Using strings in list functions...?
#14

what if you decided you want to use the .append() function? how would you do so?


#15

The end result we want is a string. So you can't use a .append function there. It's used for lists.


#16

The end result we want is a string. So you can't use a .append function there. It's used for lists.

Just to calrify, you can only append integers then, correct?? because list work with only intergers? Just trying to make sure i understand.


#17

Hi dataplayer,

List can work with any data type, see the example:

l1 = [1, 2, 3]
l2 = ["x", "y", "z", "a"]
l3 = [1, "x", True] # mixed data types
l4 = ["x", 6, 
           [1, 2, 3],  # list inside list l4
            ("y", 9)]  # tuple inside list l4

All the above lists are valid.

As you might have understood from the above, since you can have any data type inside a list, you can also append any data type to a list:

l4 = []
l4.append(1)         # int
l4.append("x")       # string
l4.append([1, 2, 3]) # list
l4.append((1, 2, 3)) # tuple
l4.append({"x": 1})  # dictionary

All the above are valid .append calls.

Hope it helps! :slightly_smiling:


#19

Thank you VERY much. This helps tremendously.


#20

3 posts were split to a new topic: 16/18 Using String Lists in Functions


#21