Ex24.py Clarification

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates

What is the value of “started”? It’s being multiplied by 500. In earlier exercises, those values were defined either by an integer or a variable; such as in ex19.py:

def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print(f"You have {cheese_count} cheeses!")
    print(f"You have {boxes_of_crackers} boxes of crackers!")

cheese_and_crackers(20, 30)

In ex19.py cheese_count and boxes_of_crackers were given both absolute numbers or variables, or both. I’m a little confused because the function (started) in ex24.py is part of an equation to define the variable jelly_beans, but has no absolute value. Is it equal to the variable “start_point”?

Hello and welcome to the forum @elvishfarmer.

The function “secret_formula(started)” takes one parameter

started

The value of “started” comes into the code when the function is called with an argument.

In this exercise 10000 is assigned to the variable “start_point”
Then the function is called with this variable as argument:

secret_formua(start_point)

So the value is: 10000

I remember struggling a bit with this exercise as it starts showing how methods can ‘model’ behaviours rather than representing it explicitly.

Initially in the book, you practice assignment based in explicit values as you pointed out. However, this exercise takes it further by not just using the method argument in its code block (to multiple by 500) but also to use an assigned variable into the argument.

I suspect the different names of the argument (started and startpoint) threw you.

Exactly! The different names made me think that it was calling a separate variable that wasn’t defined in the code. I literally rechecked my typing because I thought I missed a line!

Thank you for explaining this clearly. The value of “started” then is more of a placeholder that activates when the function is called.

Yes, that’s more or less right (for now), but you shouldn’t try to figure this out by reading the code. You have to run it and see what’s actually inside. If you’d put:

print(started)

At the top of that function you could see what value it actually has. You could also do this:

print(">>>> secret_formula", started)

Which would help you trace where it came in. Do this at the top of all the functions for any variables you’re not familiar with and you’ll figure it out by running it and see what’s going on.

Yes! I forgot to reveal the “guts” of this program. That greatly helped. I actually did a few print() lines to see where the values are stored and then I tried breaking the variable names outside the function.

Now I see what you meant in your Student Question answer for this exercise that variables are temporary inside a function.

def secret_formula(started):
print(">>>> secret_formula:", started)
jelly_beans = started * 500
print(">>>>> The value of jelly_beans:", jelly_beans)
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates

The print lines generated this output:
“>>>> secret_formula: 10000”
“>>>>> The value of jelly_beans: 5000000”

When I changed the name of the variables outside the function (and subsequently changed them on the corresponding print line…)
start_point = 10000
deans, gars, frates = secret_formula(start_point)

print(f"We’d have {deans} beans, {gars} jars, and {frates} crates.")

…That’s when I saw how the temp variable names in the function don’t have to be same. So long as there are the same amount of values to unpack.

1 Like

Nice, that’s what I’m talking about. When you post code do this:

[code]
# code here
[/code]

Yes, the variables going into a function are temporary, so that you can do new calculations without “hurting” the variables in other contexts. If you don’t have this then you can’t trust a function from someone else as they might use a variable you’re also using and then cause you problems.