I am having some trouble finding the solution for the "Join all exercise" in the Hands-on Python Tutorial of Andrew N. Harrington.
Basically, the exercise is to take a list like ['very', 'hot', 'day']
consisting only of strings, and to make one string out of it, in this case 'veryhotday'
.
'''exercise to complete and test this function'''
def joinStrings(stringList):
'''Join all the strings in stringList into one string,
and return the result, NOT printing it. For example:
>>> s = joinStrings(['very', 'hot', 'day'])
>>> print(s) # I can print s OUTSIDE joinStrings
'veryhotday'
'''
# finish the code for this function
def main():
print(joinStrings(['very', 'hot', 'day']))
print(joinStrings(['this', 'is', 'it']))
print(joinStrings(['1', '2', '3', '4', '5']))
main()
So only the first definition may be changed. I solved it like this:
def joinStrings(stringList):
return ''.join(stringList)
and I believe this works and is correct. Correct me if I am wrong here, but the problem is that it has to be solved differently. I have to sort of accumulate the different strings. just as they show here with integers:
def sumList(nums):
’’’Return the sum of the numbers in the list nums.’’’
sum = 0
for num in nums:
sum = sum + num
return sum
and really I just can't figure it out, so please help me!