“Two Fer” (One) strings and functions discussion for exercism.org solution [Spoiler Warning]

The author of the exercise probably intended for the given name to be a string, but that is not explicitly stated. The following passes all tests, and enables just about anything, including an empty string or None, to be passed as an argument, and to be included in the output:

def two_fer(*args):
    return f"One for {args[0] if args else 'you'}, one for me."

Let’s pass some genuine as well as some silly arguments:

print(two_fer()) # no name given.
print(two_fer("Monty")) # genuine name given as a string.
print(two_fer("")) # an empty string can serve as a name.
print(two_fer("this line,\nand of course")) # a name with a newline.
print(two_fer(42)) # a number can serve as a name.
print(two_fer("you")) # a third person named "you"
print(two_fer("me")) # a third person named "me"
print(two_fer(None)) # None can serve as a name.
One for you, one for me.
One for Monty, one for me.
One for , one for me.
One for this line,
and of course, one for me.
One for 42, one for me.
One for you, one for me.
One for me, one for me.
One for None, one for me.
1 Like