Is this a right way to write a function that prints x for n times? My code:
def funz(x,n):
f = (x "\n")*(n-1) x
print(f)
CodePudding user response:
You can use sep
argument of print
:
def funz(x, n):
print(*[x]*n, sep='\n')
funz('a', 3)
# Output:
a
a
a
Step by step:
>>> print([x])
['a']
>>> print([x]*n)
['a', 'a', 'a']
>>> print(*[x]*n)
a a a
>>> print(*[x]*n, sep='\n')
a
a
a
CodePudding user response:
for
loops are useful for tasks like this.
In this case you can do this:
n = int(input())
def funz(x, n):
for _ in range(n):
print(x)
funz('x',n)