nik
nik

Reputation: 11

Words should be printed one below the other but printed in one line

Result required:

Give a word: sausage

Give a number: 6

sausage

sausage

sausage

sausage

sausage

sausage

Result getting:

Give a word: sausage

Give a number: 6

sausagesausagesausagesausagesausagesausage

def repeat_this(s, n):
    print (s * n)

s = input("Give a word: ")
n = int(input("Give a number: "))
repeat_this(s, n)

Upvotes: 1

Views: 54

Answers (1)

kaya3
kaya3

Reputation: 51034

Use '\n'.join to join strings together with newlines. In this case, we need a list containing n copies of s, which can be achieved by writing [s] * n:

def repeat_this(s, n):
    print('\n'.join([s] * n))

Upvotes: 1

Related Questions