There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Under the defined function, deduct_expense, I typed everything with indentation that didn’t seem to work. So when may I use indentation for the defined function?
Because you’re concatenating two strings with new_zealand_exchange which is likely to be a float or integer depending on what you pass to that function.
You can try outside your function if you like for what happens when you do this, e.g. x = "test" + 3 and you’ll see a type error.
‘Budget’ gets referenced several times within the functions but I fail to see how this ties back to the initial variable of ‘current_budget’ and it’s value of 3500. In this instance does line 15 simply replace line 6?
Hi @emziicles. No, not 100% but having observed how Python works I’ve concluded that the first instance of ‘budget’ was declared on line 6 which returns a string and that ‘deduct_expense’ on line 9 continues to carry this through. ‘current_budget’ on line 13/14 then swaps with ‘budget’ on line 9.
Sooo, I’ve been thinking on the question about “budget” vs. “current_budget” conundrum - it almost seems like the “budget” and “expense” parameters initially passed in the print_remaining_budget and deduct_expense function definitions (lines 3, 4, 10 and 11 in my screenshot) are almost . . . placeholders? Like they serve no purpose, and the real parameters (or perhaps arguments? Sorry, I’m suuuuper new to this) aren’t used until the function is actually CALLED (lines 6 and 13)? Can anyone more knowledgeable than me confirm or deny? TIA!
Sorry for a delayed reply, you are effectively correct, they do act like that. They are names (limited to the scope of the function) that have yet to be assigned to an object (the exact implementation is largely unimportant). Parameters are unimportant outside the function but with an obvious purpose inside the function.
At some point you may come across namespaces which will make this a little clearer.
When you call your function you pass your arguments, the objects these arguments reference are then assigned to the parameters inside the function, the function executes, and the assignment is forgotten once the function completes.
def func(value): # value is our parameter
return value
func(3) # here an integer 3 is our argument
# an integer object, 3, is assigned to the name 'value' for the duration
# of the function and this assignment is forgotten once the function exits
Hi all,
I’d like to ask this since I’ve been looking at this page for days now and still wonder about this point:
We’re given a current_budget = 3500.75 at the beginning. Then our current_budget becomes just budget, though! Why is that?
def print_remaining_budget(budget):
print("Your remaining budget is: $" + str(budget))
print_remaining_budget(current_budget)
I must say I haven’t been working consistently during the last month and it could be a silly question… My apologies if that’s the case. And thanks for your help anyway!
It’s a parameter of that function, that is, a name which only exists in the scope of that function. By passing current_budget as the argument you are assigning budget to the value (the object really) referenced by current_budget for the duration of the function. It’s not overly different from doing the following-
You’ll likely come across more detail on functions as the course goes on but you could equally write print_remaining_budget(budget=current_budget) for your last line for the same result (this would be using it as a keyword argument, you’ll run into them eventually).
That name budget is limited to the function and is assigned when the function is called and forgotten again when the function exits.
For me it’s an issue within the exercise : It seems like the correction of the exercise wants deduct_expense function’s first parameter to be named current_budget and not budget … for me it’s a mistake as it can be misleading with the global function current_budget (i would let each local functions to use budget). So if there is any kind moderator that wants to look into this that would be much appreciated.
I’m still a little bit confused as to what the purpose of the return function is, or when to use it. Why can’t the “calculate_exchange_usd” or “def deduct_expense” functions be used without “return”?
You can write those functions without using return, but returning values is a handy way to pass useful information back to the function call. Say you needed to perform some set of mathematical operations on multiple numbers and that you also needed to store the results of the new numbers. By using return in this case, you would be able to store the new numbers in variables by using something like:
Couldn’t figure out why terminal gives me the ‘None’ lines. (I have added some numbering 1 and 2 to the two various ways of printing the result, just to see any difference). Much appreciated if anyone knows.