Why does adding a word to an array of others words in dictionaries split the word into single letters?

Hello, I’m super confused, why would this function after being fed arguments:
play_word(“Blue”,“sveikas”)

returns:
{“Blue”, [‘earth’, ‘random’, ‘s’, ‘v’, ‘e’, ‘i’, ‘k’, ‘a’, ‘s’]}
and not just:
{“Blue”, [‘earth’, ‘random’, ‘sveikas’}

earth and random being already in the dictionary values for Blue from earlier

‘’’
def play_word(player, word):
player_to_words[player] += word
return player_to_words
‘’’

I’m pretty sure it’s something I don’t quite remember from syntax, but I legit have no idea how to fix it :c Greatly appreciated~!

Do you mean something like this behaviour:

>>> a = []
>>> a += "hello"
>>> a
['h', 'e', 'l', 'l', 'o']

strings are iterable and a whole lot of things in python are happy to accept an iterable as input.
But when you do that, then you’re not giving it a string, you’re giving it some iterable of characters.

So, consider what you asked for to happen. In my example, if I wanted to add a string to the list, then I would append the string to the list.

>>> a = []
>>> a.append("hello")
>>> a
['hello']
>>> max("hello")
'o'
>>> max("hello", "there")
'there'

It’s not sufficient to pick any random operation and use a string with it and hope to get the right result.

2 Likes

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