Make a reusable function
Define a function that returns a result and accepts a default parameter.
Give repeated logic a name
def starts a function definition. Parameters name the inputs; arguments provide their values at a call site. The body runs when the function is called, not when Python reads its definition. A short docstring can explain the contract to the next person using it.
return sends a value back to the caller and ends that call. print only writes text. A reporting function that returns a string can be printed, tested, written to a file, or reused by another function. A function without a return statement returns None.
Defaults and named arguments
A default such as site="unknown" is used when the caller omits that argument. Named arguments like site="Sydney" make call sites easier to read. Prefer immutable defaults such as strings or numbers; a mutable list used as a default is shared across calls.
Names created inside a function are local to that call. Pass inputs explicitly instead of relying on an unrelated terminal variable. The checks call your function with several values, including a custom site and Unicode text, so a hard-coded example will not pass.
def describe_host(name, site="unknown"):
return f"{name} @ {site}"
print(describe_host("db-01", site="Sydney"))
Put it into practice.
Define describe_asset(name, memory_gb, site="unknown") returning "{name} | {memory_gb} GB | {site}". Return the string; the function itself should not print it. Demonstrate a call outside the function if you like.
See an explained solution
def describe_asset(name, memory_gb, site="unknown"):
return f"{name} | {memory_gb} GB | {site}"
print(describe_asset("web-01", 16))
The function returns a value without a display side effect. Its caller decides what to do with that value, and the default keeps simple calls concise.