Using a loop to concatenate words instead of using what is suggested to everyone learning python


#1

Pretty sure this has come up aleady but have not found a recent discussion where this was covered.

In lesson 16, the user is tasked to created a function which concatenates words and is expected to use a for loop. Why is the user encouraged to do this task in a non-optimal way?

The excercise is marked successfully using the following code

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

print join_strings(n)

the problem with this is that it shows poor programming skills. Even if you argue that it should demonstrate a different concept, this example doesn't lend itself very well to demonstrate without mentioning that this is not the way beginners are advised to concatenate strings. At least mention the more pythonic way, which is to use the .join built in function.

Thanks.


#2

yes, but if you use built in functions for everything, you will never grasps the concepts.

of course you could use join:

def join_strings(words):
   return "".join(words)

of course join would be a better approach, but if you never get familiar with the underlying concepts, your understanding will be poor, you will never make a good programmer either


#3

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