2017-01-10 23 views
-2

這是假設創建一個函數,該函數可以返回重複n次的字符串,並由用戶需要的字符串delim分隔。我錯過了什麼?如何編程函數返回重複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() 

回答

0

您必須將字符串添加到最後:

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