0

My program is to return the string repeated n times separated by the string delim. example repeat("ho",3,",").

def repeat():
    string=input("enter a string:")
    n=int(input("enter how many times to repeat:"))
    delim=(",")
    return string,n,delim
print (repeat())

I need to change the output to (hi,hi,hi) instead of this.


enter a string:hi


enter how many times to repeat:3


('hi', 3, ',')

1
  • The only problem im having now is that I cant get it to print by string with , inbetween each one. Commented Oct 8, 2013 at 21:07

1 Answer 1

1

I would recommend using the .join() method of strings to concatenate your strings. For example:

', '.join(['yo']*4)
Out[4]: 'yo, yo, yo, yo'

There's some other issues in your code, the biggest of which I'd say is that your method repeat() should not be responsible for taking user input. Move that into main() and delegate repeat() to only performing string operations.

Sign up to request clarification or add additional context in comments.

1 Comment

what do I return into main, so that i do not get the error that string=input is not defined?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.