Challenge - 5 Problems
Docstring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput of a function with a docstring
What is the output of this code when calling
greet('Alice')?Python
def greet(name): """Return a greeting message for the given name.""" return f"Hello, {name}!" print(greet('Alice'))
Attempts:
2 left
๐ก Hint
The function returns a formatted string using the input name.
โ Incorrect
The function greet returns the string 'Hello, Alice!' when called with 'Alice'. The docstring does not affect the output.
โ Predict Output
intermediateAccessing a function's docstring
What will be printed by this code?
Python
def add(a, b): '''Add two numbers and return the result.''' return a + b print(add.__doc__)
Attempts:
2 left
๐ก Hint
The __doc__ attribute holds the function's docstring.
โ Incorrect
The __doc__ attribute returns the string inside the triple quotes of the function definition.
โ Predict Output
advancedDocstring effect on function behavior
What is the output of this code?
Python
def multiply(x, y): '''Multiply two numbers.''' result = x * y print(multiply(3, 4))
Attempts:
2 left
๐ก Hint
Check if the function returns a value.
โ Incorrect
The function does not return anything explicitly, so it returns None by default.
๐ง Conceptual
advancedPurpose of docstrings in Python
Which of the following best describes the main purpose of a docstring in Python?
Attempts:
2 left
๐ก Hint
Docstrings are meant for explaining code, not running it.
โ Incorrect
Docstrings provide helpful explanations about code to anyone reading or using it.
โ Predict Output
expertDocstring and help() function output
What will be the first line printed by
help(divide) for this function?Python
def divide(a, b): '''Divide a by b and return the result.''' if b == 0: raise ValueError("Cannot divide by zero") return a / b
Attempts:
2 left
๐ก Hint
The help() function prints a header line before the docstring.
โ Incorrect
The first line printed by help() shows the function signature and location before the docstring.
