Can Python work out two ticket totals without changing the total you already saved?
You're writing a ticket calculator for a GAA club. Your main program has a variable called total, and your ticket function uses that same name. When you call the function twice, what happens to the total you already saved?
You already know the route through a function. You call it with an argument, the function runs its instructions, and a return statement sends a value back. If you want to use that value later, you can save it in the caller.
Fare multiplies tickets by three and returns total. Call fare with two: its tickets is two, its total is six, and print shows six. Call it with four: its tickets is four, its total is twelve, and print shows twelve. Each call gets its own parameter and local total.
Suppose you call fare with two tickets, then call it again with five. Just before the second call returns, what values do tickets and total have inside that call? Pick the pair.
Inside fare, total holds the ticket cost. Outside fare, the main program sets its own total to twenty. Each call sets a separate local total, so the outer total stays at twenty. That's local scope: in fare, the name total refers to the variable for that call.
Set the outer total to twenty. In the first call, three times three gives a local total of nine. The outer total stays at twenty, and the first print shows nine. In the second call, five times three gives fifteen. The outer total still stays at twenty. The final print reads it.
Keep the same fare function, but start with an outer total of eight. You print fare of two, then fare of four, then total outside the function. Pick the three numbers in the order they'll appear.
Go back to the example where the outer total was twenty. It's tempting to carry fifteen from the second call into the final print because both variables are called total. That final print runs outside fare, where total still refers to the variable set to twenty.
Write down the output of this pseudocode. Fare triples the ticket count and returns its local total. Set the outer total to twelve. The first output uses the return value, six. The next reads the outer total, twelve. Now store the second return value in the outer total. The last output is six.