如何编写函数 return 字符串重复 n 次,用字符串 delim 分隔

How to program a function to return the string repeated n times, separated by the string delim

这是假设创建一个函数,可以return 字符串重复n 次,由用户所需的字符串delim 分隔。我错过了什么?

def repeat(string, n, delim) :
  return (string + delim) * (n - 1)


def main() :
  string = input("Enter a string: ")
  n = int(input("Enter the number of times repeat: "))
  delim = input("Enter the delim: ")

main()

您必须将字符串添加到最后:

def repeat(string, n, delim) :
  return (string + delim) * (n - 1) + string


def main() :
  string = input("Enter a string: ")
  n = int(input("Enter the number of times repeat: "))
  delim = input("Enter the delim: ")
  print(repeat(string, n, delim))

main()

输出:

Enter a string: hello
Enter the number of times repeat: 10
Enter the delim: ,
hello,hello,hello,hello,hello,hello,hello,hello,hello,hello