This forum is now read-only. Please use our new forums! Go to forums
15/18 Iterating over a list in a function
Hi! Here is my code for this exercise:
n = [3, 5, 7]
def total(numbers):
result = 0
for i in numbers:
result += i
return result
This worked but I would like know how to use a “ for item in range()” loop to do this. Because I tried “ for i in range(len(numbers))” that in the first place and it failed.
Thank you!
Answer 553b7d2ce0a3007feb000021
When you post code, always format it by selecting the code, and then clicking the {}
button above the editing area, so that we can see important details, such as the indentation and underscores.
To use …
for i in range(len(numbers)):
… you can do this …
def total(numbers):
result = 0
for i in range(len(numbers)):
result += numbers[i]
return result
Here, i
serves as an index to numbers
, so, to access individual items in the numbers
list with indexing syntax, use numbers[i]
.
Answer 554690f5e39efe77830003d1
I got this to work:
n = [3, 5, 7]
def total(numbers):
result = 0
for i in range(len(numbers)):
result = result + numbers[i]
return result
print total(n)
2 comments
I have the exact same code, but it doesn’t work
Never mind, I found my mistake.
Answer 5548372076b8fe79490004e1
I got this to work but I’m not entirely sure why.. n = [3, 5, 7]
def total(numbers): results = 0 for i in range(len(numbers)): results = sum(numbers) return results
6 comments
i was playing around with yours. it returns 15 regardless of the actual range that the loop is iterating over. E.g., if you just had the loop run over two items in the series, it still returns 15, even though it should return 8. i think it’s just pure luck that it’s accepting it.
@Austin Slack How comes it 8? [3, 5, 7][3, 5, 7][3, 5, 7]then sum([3,5,7])results =15.
You have a list w/ three items [3,5,7]. With the expression “result = result + numbers[i]” or “result += numbers[i]”, you’re loop would look like the following (remember we’re initializing at results = 0):
Loop 1: result = 0 + 3 => result now = 3, Loop 2: result = 3 + 5 => result now = 8, Loop 3: result = 8 + 7 => result now = 15.
But if the expression in your loop is “sum(numbers)” instead of “result = result + numbers[i]” or “result += numbers[i]”, you won’t get this incremental increase in the value of results. Instead, regardless of how items you loop through, it will return 15.
i played with the “sum(numbers)” expression yesterday, and regardless of how many items I looped through, it returned 15 every time. I was expecting that if I looped once, it would return 3; if I looped twice, it would return 8; and if I looped three times, it would return 15. But it returned 15 every time.
It returned sum of the list.
exactly, but what you want to do is write a loop that returns the sum of the list for different points in the list – e.g., to return 3 when you stop the loop at the first item, to return 8 when you stop the loop at the second item, and to return 15 when you stop the loop at the third item. but “sum(numbers)” doesn’t do that; it just returns the sum of the entire list regardless of whether you stop the loop at the first, second, or third item.
Answer 5549ea1195e3788c7c0001c7
http://www.codecademy.com/glossary/python/list-comprehensions
[x for x in []]
def total(x):
return sum(x)
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
8 comments
Thank you Glenn! I get it now. The reason was I didn’t add the index to the result. (And now I know how to edit my code with {} when posting a question :D Thanks).
n = [3, 5, 7]
def total(numbers): result = 0 for item in range(0, numbers): result = result + numbers(item) return numbers(item) WHY IS THIS WRONG THEN!!
@Abhinandanudupa, you can do like you described, but you need to return result, not numbers(item):
n = [3, 5, 7]
def total(numbers): result = 0 for i in range(len(numbers)): result = result + numbers[i] return result
print total(n)
Why does i serve as an index to numbers, as opposed to indexing the individual values in the range, like it normally does with a list?
i is a variable that represents an item in the list, and by extension, i guess it sort of acts like an index too.
“numbers” is the argument for the “total” function. It is also a list. In the “for” clause of the loop you can refer to the items in list any way you want – e.g., “i”, “item”, “number” – but after the for clause, you must use the list[item] notation – e.g., numbers[i], or numbers[item], or numbers[number].
Not sure if I’m answering your question.
Thank you
Thank you!!!!
Right, thanks, I started losing some hair over that one.