Nope, you don’t need to create a
cost variable anywhere outside of the function to use it inside the function. This isn’t specific to Python, most programming languages will let you create variables inside of functions which remain visible only to the code inside the function. This is called scope.
Let’s say you define the following function:
def raise_to(number, power):
result = number ** power
In that code, we have defined a function
raise_to which calculates the value of whatever you pass as
number raised to a certain
raise_to(5,2) would return 25 (or 5 x 5).
result only exists within that function, and isn’t available to use anywhere else in the larger body of code where that function appears.
Does that help?