-3

I've read several questions similar to this, but still can't quite figure this out. This should be simple, but I'm not seeing the forest from the trees.

I have a for loop in a function as follows:

    for item in range(len(mf)):
        fp = fp + str(mf[item])
    return(fp)

I then want to call the function in my main function and return however many instances of fp there are like:

print(return_1) print(return_2)

Ryan
  • 19
  • 9
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. – Prune Apr 28 '17 at 16:09
  • In particular, there *is* only one instance of **fp**. Even if you were to remove the return from the loop, you'd have only one instance. Provide sample calling sequence and expected output? – Prune Apr 28 '17 at 16:10
  • My apologies for the vagueness folks. Very new to Python and programming, in general. I do realize now that this loop only runs once, which is not what I wanted. The range will vary from one run to the next, so I need to be able to return as many values as are produced in the called function and then print each of them out separately in the calling function. Does this help? – Ryan Apr 28 '17 at 16:35
  • To add further clarification the calling function is simple and will be something like: print (return_1), print(return_2), etc. – Ryan Apr 28 '17 at 16:49

1 Answers1

1

Take the return statement out of the loop. You don't want to return until you've finished concatenating all the items.

You can also replace the loop with a list comprehension and the join function.

return "".join(str(item) for item in mf)
Barmar
  • 741,623
  • 53
  • 500
  • 612