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, ',')

4

1 回答 1

1

我建议使用字符串的.join()方法来连接你的字符串。例如:

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

您的代码中还有一些其他问题,我想说的最大问题是您的方法repeat()不应该负责接受用户输入。将其移入main()并委托repeat()给仅执行字符串操作。

于 2013-10-08T20:54:36.783 回答