Reputation: 1
My apologies for asking dumb question.
I am learning Python through videos and books and really have no other help.
Can't figure out the simple differences on basic python programming.
########################################AAAAAA costs = [5,10,15,20]
def sum_cart(item):
total_cost = 0
for this in costs:
this = this + total_cost
return total_cost
print sum_cart(costs)
########################################BBBBBB
def sum_cart(item):
total_cost = 0
for this in costs:
total_cost = this + total_cost
return total_cost
print sum_cart(costs)
#########################################CCCCCC
def sum_cart(item):
total_cost = 0
for this in costs:
total_cost = this + total_cost
return total_cost
print sum_cart(costs)
-----------QUESTION-----------
The result is A --> 0, B --> 50, C --> 5
I am still confused why the result appears as is.
If my understanding is right, in A, 'this' gets 5 from the list, and 5 is added to total_cost, which is 0. 'this' then calls 10, 15, and 20 respectively and 'this' gets a new value. However, because the total_cost is still 0, the result is 0. Am I right?
Then in B, the total_cost is updated when 'this' = 5 is called and added to the current 'total_cost' = 0, which is 5. The loop goes back and brings in 10, 15, and 20 respectively and the 'total_cost' is updated to 50. So far so good, I think.
But then in C, I am unsure what's going on because 'total_cost' is updated when 'this' brings in value of 5 from the list. It then should returns 'total_cost' to 5. Shouldn't the for-loop go back and do total_cost = this (supposedly 10) + total_cost (currently 5) and do the loop again? What am I missing about the "return" function?
Upvotes: 0
Views: 374
Reputation: 1
In C as soon as loop go to return part it exits from the function and print the first value which total_cost have received. But there is a major problem with all the three options of yours. You are passing the cost list as a parameter(item) but still you are using global variable cost in the loop.
def sum_cart(item):
total_cost = 0
for i in items:
total_cost += i
return total_cost
Upvotes: 0
Reputation: 55
In example A- you are summing values of this in this and returning total_cost, which remains 0.
In example B- it is semantically correct. You are summing values of list in total_cost and returning total_cost.
In example C- the return is in for loop so the first value of the list is summed in total_cost and the total_cost returned.
Upvotes: 0
Reputation: 1
Actually, Code A looked close to the right answer except that your parameter in the function is 'item' and you're iterating over a list 'costs'. And the logic can be corrected as total_cost+=this since we're interested in incrementing the total_cost than 'this' variable. You can try out this code;
costs = [5,10,15,20]
def sum_cart(costs):
total_cost = 0
for this in costs:
total_cost += this
return total_cost
print sum_cart(costs)
Hope this answer helps...
Upvotes: 0
Reputation: 860
In case C:
As soon as the first iteration in the loop hits 'return', the function ends. The loop was built inside of the function, and the function has ended. Therefore, the loop stops.
The value returned is total_cost at the end of the first iteration: 5.
Upvotes: 0
Reputation: 11534
All three have something wrong with them as written. You should write something more like this:
costs = [5,10,15,20]
def sum_cart(items):
total = 0
for item in items:
total = total + item
return total
print sum_cart(costs)
Even your example B, though it gets the correct answer is wrong in that you're passing in the parameter item
, but in your loop you're looping over the global variable costs
(so you're not actually using the parameter you passed to your function).
Upvotes: 1
Reputation: 1121476
You are correct in your first two assumptions.
In C, return
exits the function immediately. The loop is aborted at that point, returning total_cost
(which is 5, at that point).
Upvotes: 2
Reputation: 2026
Shouldn't it spell
for this in item:
instead of
for this in costs:
?
Upvotes: 0
Reputation: 5997
In sample A, total_cost was never not zero. You took the value zero from total_cost and added it to something else - which would not change the value of that one either.
In sample B, you are adding whatever the value of this
is to total_cost each time. Thus it will accumulate values as you go along. Whatever is on the left side of an equal sign in python takes the value of the expression on the right.
In sample C, your indentation is different and the return statement is inside of the for loop. This will cause the function sum_cart to return the current value of total_cost as soon as it hits this line of code (the first time through your for loop). Return doesn't just boot you out of the current iteration of a loop, it boots you out of whatever function you are inside of because return literally means "go back to wherever I (I being the function here) was originally called from. You can optionally return a value, like you are doing here.
Upvotes: 0
Reputation:
return
always ends the execution of the function that it is found in.
Thus, in your last example, as soon as the function reaches return
, the sum_cart()
function is going to come to an end, returning total_cost
(which, the first time it's encountered, would be equal to 5.
Notice that in the other two examples, the return
statement is located outside of your for loop, which is why they don't return the value until they've finished iterating over your array.
Upvotes: 0
Reputation: 251355
return
returns from the entire function and ends it. In your third example, the for loop only gets to run once. At the end of its first run, the function returns and the rest of the for loop never happens.
Upvotes: 0