2017-01-10 10 views
-2

これは、ユーザーが希望する文字列delimで区切られた文字列をn回繰り返して返す関数を作成すると仮定します。私は何が欠けていますか?文字列で区切られた文字列を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 
関連する問題