Keyword-only Arguments

Sometimes, you want to specify arguments for a function by their name - making it clear what each argument represents, regardless of their position. In Python, these arguments are referred to as keyword arguments. Keyword-only arguments are similar, but with a special twist: they can only be provided by keyword, never by position.
To define a function with keyword-only arguments, you use the * symbol in the function signature. Any arguments appearing after * can only be provided by their keyword.
Consider a function to calculate the area of a rectangle. It requires two arguments: length and width. In addition, it can accept an optional boolean argument, rounded, to decide whether to return the exact area (with decimals) or a rounded area (no decimals). We want rounded to be a keyword-only argument, to prevent confusion when calling the function:
def rectangle_area(length, width, *, rounded=False):
    area = length * width
    return round(area) if rounded else area

print(rectangle_area(5.3, 4.2))                # Prints 22.26, `rounded` is not provided so it defaults to False
print(rectangle_area(5.3, 4.2, rounded=True))  # Prints 22, `rounded` is provided and set to True
print(rectangle_area(5.3, 4.2, True))          # Error! `rounded` must be provided by keyword.
print(rectangle_area(5.3, 4.2, False))         # Error! `rounded` must be provided by keyword.
print(rectangle_area(length=5.3, width=4.2, rounded=True))  # Prints 22
We can call this function using positional arguments for length and width, and a keyword argument for rounded. If someone tries to provide the third argument by position, an error occurs.
Note that you can still use the keyword syntax for positional arguments if you want to as they are not defined as positional-only here.

Challenge: Repeat the Text

You are given the task of writing a function repeat_text that repeats a specified text a certain number of times. The function should take two arguments: text and times. The text argument is the text string to be repeated and times is the number of repetitions.
The times argument should be a keyword-only argument with a default value of 1, which means that if the times argument is not provided, the function should return the original text as it is.
The function should return a string that is the text repeated times number of times.
The first line of the input contains a string text. The second line contains an integer times.
The function should return a string which is the text repeated times number of times.
Hello World! 3
Hello World!Hello World!Hello World!
Note: There is no space or any other delimiter between the repeated texts in the output.


Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 10 MB

To check your solution you need to sign in
Sign in to continue