+ 1

how to get this >>> explode("spam") ['s', 'p', 'a', 'm'] >>> explode(" ") [ ]

14th Sep 2016, 5:15 PM
Vinh Le
Vinh Le - avatar
13 Réponses
+ 3
>>> list("spam") ['s','p','a','m',] >>> list("") []
14th Sep 2016, 5:25 PM
Giovanni Gatto
Giovanni Gatto - avatar
14th Sep 2016, 6:26 PM
Zen
Zen - avatar
+ 2
Please stop just saying "It doesn't work" without giving any clue of the problem, it's irritating. We're not psychics, we can't read your mind. Please? Could you copy/paste what you get and what you expect?
14th Sep 2016, 6:59 PM
Zen
Zen - avatar
+ 1
def explode(s): if not isinstance(s, str): raise TypeError chars = [] for c in s: chars.append(c) return chars
14th Sep 2016, 5:32 PM
Zen
Zen - avatar
+ 1
How so?
14th Sep 2016, 5:53 PM
Zen
Zen - avatar
0
There are something wrong, it doesnt work right
14th Sep 2016, 5:52 PM
Vinh Le
Vinh Le - avatar
0
I dont know, that whys im asking you guys, I try to run your but it doesnt work the same the given output :(
14th Sep 2016, 6:14 PM
Vinh Le
Vinh Le - avatar
0
Oh thats my fault, my question was unclear so your answer went to another way but you're right. My qs is that explode(S) should take string as input and should return a list of characters in that string, so the qs was just a example of an output
14th Sep 2016, 6:37 PM
Vinh Le
Vinh Le - avatar
0
I put that in to the python on my computer, it doesnt work, i dont know why
14th Sep 2016, 6:51 PM
Vinh Le
Vinh Le - avatar
0
"Oh thats my fault, my question was unclear so your answer went to another way but you're right. My qs is that explode(S) should take string as input and should return a list of characters in that string, so the qs was just a example of an output" I still don't see how the explode() I defined is different from the one you asked for. Unless you mean that the list can't have repeated characters? Giovanni nailed it btw, you can simply use list().
14th Sep 2016, 6:52 PM
Zen
Zen - avatar
0
# remember the indentation def explode(x): list(x) explode("spam") explode("")
15th Sep 2016, 5:07 AM
Giovanni Gatto
Giovanni Gatto - avatar
0
# even so explode = list explode("spam") explode("")
15th Sep 2016, 5:10 AM
Giovanni Gatto
Giovanni Gatto - avatar
0
# if, for any reason, you don't want to use 'list' def explode(x): c = [n for n in x] return c explode("spam") explode("")
15th Sep 2016, 5:15 AM
Giovanni Gatto
Giovanni Gatto - avatar