Home > other >  How can I repeat a function?
How can I repeat a function?

Time:11-09

I want to define a function which accepts two arguments: n and s. The function should print s a total of n times. Then I want to se the function defined to print Hello world! k times where k is a random integer between 1 and 100.

This is my code:

def funz(s,n):
    x=((((s*n) '\n')*n)[:-1])
    return x

def repeat(s,n)
    for k in range(1,100):
    return ('Hello world!')

Can you help me correct what I am doing wrong?

CodePudding user response:

Use random.randint() to select a random number from 1 to 100, then pass this as the n argument to funz()

import random

def funz(s,n):
    x = (s "\n")*(n-1)   s
    return x

print(funz("Hello world!", random.randint(1, 100)))
  • Related