I’m attempting to get through the Sentence Smash Kata on CodeWars, I’m pretty new to coding and I am wondering why my code isn’t properly running.
My code
def smash(words):
a = ['hello', 'world', 'this', 'is', 'great']
return " ".join(a)
smash("")
I can’t remove the "words" parameter and I think I have to instantiate it but I don’t know in what way I should do that. I thought substituting the argument for quotes would work and it did run but none of my tests passed.
>Solution :
When you define a function, everything within the (...) is an argument (or a parameter, there is probably some specific technical distinction, but either is generally fine). Those are variables that are then usable within the scope of the function, and they are set when you call the function with whatever you have in the () when you call.
This framework allows you to make the general function and then do specific things with it based on what you pass in.
In your case, what you probably want is this:
def smash(words):
return ' '.join(words)
my_words = ['hello', 'world', 'this', 'is', 'great']
smash(my_words)
In the scope of the function, you have the variable words and you attempt to join the pieces with a ' ' inbetween.
Then, outside the scope of the function, you declare a variable with an actual, concrete list of words, and then call the function using that concrete list.