I decided to add a second answer in response to this comment under BcK's answer:
Also, I'm not allowed to change any of the code below the process_order() function.
This... is technically doable, but the question is subsequently kind of strange to me. In order for the code below the definition of process_order
to work without modification, you have to make a change to how you define total
. I find this strange because changing total
is only permissible due to the fact that it's "technically above the definition of process_order
" which seems like a pretty weak excuse to me.
The crux of the problem with total
is the intuitive answer would be:
def process_order(x_list):
{do stuff}
total += quantity * cost
The reason this solution doesn't work is because assigning a variable within a scope (in this example, the total =
segment of total +=
) creates a reference that supersedes the reference of any "higher" scopes. This means that total
either cannot be assigned within the scope of process_order
(technically achievable given the current restrictions, but requiring a bit of hackery), or a reference to the total
variable that exists in the outer scope needs to make its way into the inner scope.
The simplest way to achieve the latter is to use the global keyword. I really, really don't like the global
keyword (and there are people who agree with me), but for whatever reason it keeps popping up, so here's how you would use it (as well as an alternative way of writing process_order
for your learning pleasure).
## Declare total as global variable
global total
## Set total
total = 0
def process_order(x_list):
""" Remove an an order item (as a tuple (name, quantity, cost) ) from x_list and add it to the global total"""
## Let the interpreter know that you plan on using total in this scope
global total
## List.pop is a good way to incrementally destroy a list
## (you can use an index as an argument: the default is -1)
item = x_list.pop()
## This takes advantage of sequence unpacking and multiple assignment
(name,quantity,price) = item
## You can now assign to total because it has been declared as a global variable
total += quantity * price
## The rest is unchanged
x = [("oranges", 4, 3.22),("gummy bears",1,1.99),("sour bites", 3, 2.33), ("antacid", 1, 5.33)]
while(len(x)>0):
process_order(x)
print("total price: ", total)
## Outputs: >> total price: 27.19
All that being said, I would sooner assume that there was some miscommunication in the assignmet; changing the while
loop to match BcK's example would make significantly more sense, in my opinion.