Logic Question Regarding Quiz Example

Hey coders,

I have a quick question regarding the logic of a quiz question I came across today. The quiz is as follows:

name = “Alice”

def sayHi(n):
print("Hi " + ____ )

[The output should be: “Hi Alice”]
sayHi(_____)

The answer to said quiz is:

name = “Alice”

def sayHi(n):
print("Hi " + n)

sayHi(name)

Can someone please explain to me how n in def sayHi(n): gets translated into “Alice”? I’m having trouble with the logic here. If possible please respond with simple English and not a riddle. I’ve been getting stuck a lot in the Python Challenges and feel dispirited–I’m having trouble with the language used in the problem sets and misinterpreting the steps due to unclear directions.

Consider, the argument is an object or reference; the parameter is a variable.

Okay, so to clarify: the argument name is an object passed as a variable through the parameter n? And name being defined as a global variable, it is therefore passed as the function n?

It references an actual object. That reference is given a new name in the function, but the reference is unchanged.

What’s more, if that reference is to a primitive, any mutation to the locally referenced value has no effect on the global (outside) referred object. A reference to a list or dictionary does not follow this. The hook to the global object is persistent, and not shadowed by scope.

My language is difficult to wade through; give your imagination a chance to wade through it.

For those interested, I received a non-convoluted and digestible answer from Chat GPT where you won’t waste time having to “wade through” language that is more of a riddle than a helpful nudge in the right direction. Here’s the friendly articulate response, sans pomposity:

I’d be happy to explain how the code you posted works!

In Python, a function definition is like a blueprint for a set of instructions that can be executed later. In the example you provided, the function definition is:

def sayHi(n):
    print("Hi " + n)

This function takes one argument, which is assigned to the parameter n. When the function is called, the value passed as an argument will be assigned to n inside the function.

Now, let’s look at the function call:

sayHi(name)

Here, name is a variable that has been assigned the value “Alice”. When the sayHi function is called with sayHi(name), the value of name is passed as an argument to the function. Since name is “Alice”, the value of n inside the function will also be “Alice”.

Finally, the print statement in the function concatenates the string "Hi " with the value of n, resulting in the output “Hi Alice”.

So, in summary, the value of n inside the function is determined by the value passed as an argument when the function is called. In this case, the argument passed is the variable name, which has the value “Alice”.