0

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?

BenMorel
37.1k53 gold badges208 silver badges339 bronze badges
asked Aug 21, 2012 at 19:49
3
  • on a side note, codeacademy provides a pretty decent python tutorial. you might want to have a look. Commented Aug 21, 2012 at 20:05
  • On a second side note, the Python standard library comes with a built-in sum() function that works for lists and other iterables. Be sure to keep the Python standard library manual (docs.python.org/library) handy, because learning it is a significant part of learning the language. Usually it is installed locally on your machine when you install Python. Commented Aug 21, 2012 at 20:32
  • Thanks for everyone's input, especially Martijn & John. It surely cleared alot of things. I thought something was wrong, just couldn't find it where it was. BTW, it was from this tutorial, but I guess I wouldn't recommend it anymore to anyone since there are whole lot of errors. [http:// goo.gl/ex8zG] Commented Aug 22, 2012 at 21:46

10 Answers 10

2

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).

answered Aug 21, 2012 at 19:53
Sign up to request clarification or add additional context in comments.

Comments

1

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).

answered Aug 21, 2012 at 20:06

Comments

0

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.

answered Aug 21, 2012 at 19:52

Comments

0

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.

answered Aug 21, 2012 at 19:53

Comments

0

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.

answered Aug 21, 2012 at 19:56

Comments

0

Shouldn't it spell

for this in item:

instead of

for this in costs:

?

answered Aug 21, 2012 at 19:56

Comments

0

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.

answered Oct 12, 2015 at 13:27

Comments

0

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...

answered Jan 15, 2021 at 6:22

Comments

0

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.

answered Jan 15, 2021 at 6:38

Comments

0

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
answered Aug 26, 2021 at 18:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.