Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get my smash function to run and return my combined list?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading