0
[SOLVED] Unexpected None value in solution. Adding Words problem attempt.
I was attempting to solve the Adding Words project for python and wrote the below code: def concatenate(*words): """Takes a string of words and concatenates them together with a '-' separating the words""" for word in words: print(word, end='-') print(concatenate("I", "love", "Python", "!")) Whenever I run this code python is adding a None value to the string i.e. I-love-Python-!-None Can someone explain why the None value is being added to the end of the string?
10 Réponses
+ 8
Since your function contains a print function, you don't need to print it. Just call it, i.e.
concatenate("I", "love", "Python", "!")
You get None when you try to print a print.
How are you going to get rid of the final "-"? 😁
+ 5
If you don't want to use "join", here is an alternate solution using native loop.
https://code.sololearn.com/c1CqwQVKc6U6/?ref=app
+ 2
Oh ok that makes sense. Thanks David Ashton.
I'll have to go back and change my code, cause it looks like I have more problems than just the None value.
+ 1
A similar question for this same problem was asked just a few days ago.
https://www.sololearn.com/Discuss/2583975/?ref=app
+ 1
1 - def name(*args):
2 - x = "-".join(args)
3 - print (x)
4 -
5 - name('I','love','Python','!')
0
Cool, thanks i'll have a look
0
def concatenate(*args):
return '-'.join(args)
print(concatenate("I", "love", "Python", "!"))
0
Really? It was that easy!? I was close anyway 😏 Thanks for the help guys, certificate here I come
- 1
What an easy 100 XP!
print("I-love-Python-!")
🤣
- 1
def concatenate(*args, sep="-"):
return sep.join(args)
Print(concatenate ("i", "love", "python" ,"!"))