15/18 Iterating over a list in a function: Got it to work but wondering why it works that way


#1

Link to exercise:
https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/iterating-over-a-list-in-a-function

Console Error Msg:
total ([0,3,6]) returned 0 instead of 9

Info:
I put 2 codes below the 1st is the one that didn’t work and the 2nd is the one that worked. The only difference between the 2 is where I put the “return result” inside or outside the for loop.

Question:
So what’s the logic behind my 2nd code working vs the 1st code not working?

#My 1st attempt didn't work (return is inside for loop)

n = [3, 5, 7]    #Code Academy isn't using this list but instead it's own behind
                 #the scene list of [0, 3, 6]
                 #Which is why the error msg does not correlate with this list
def total(numbers):
  result = 0
  for x in range(0,len(numbers)):
    result += numbers[x]
    return result    #<----Difference is here
  print result
#My 2nd attempt it worked (return is outside for loop)

n = [3, 5, 7]    #Code Academy isn't using this list but instead it's own behind
                 #the scene list of [0, 3, 6]
                 #Which is why the error msg does not correlate with this list
def total(numbers):
  result = 0
  for x in range(0,len(numbers)):
    result += numbers[x]
  return result    #<----Difference is here
  print result

Off topic question:

I’ve downloaded python from https://www.python.org/downloads/
version 3.6.3 but when I fired it up the program seems nothing like what’s on code academy namely there is no editor with a interpreter next to it. Instead there is just 1 console that seems to execute my code as soon as I press enter to type my next line of code. So I can’t even structure or look at the entirety of my code before it’s executed. I noticed the syntax is a bit different there too such as “print” in Code Academy is print() in Python 3.6.3

Is there a way to get a editor and interpreter side by side like on code academy?

Why’s the syntax different from code academy?

Which python version listed above is most similar to code academy’s syntax?

Which version of python should I be using 3.6.3 or 2.7.6 and 32 bit or 64 bit (if it matters)?

Getting this thing setup would really help with me practicing my own code and debugging it since code academy has a built in script that runs its own code to check if you did things correctly. So I couldn’t really do much outside of what’s expected or it’ll error out.


#2

36 PM

Edit your code in the window, and save it with a .py extension on the file name.

To run the program. go to the Run menu and choose Run Module.

Codecademy uses Python 2.7, which differs somewhat from Python 3.6.3, but in most respects, they are similar. Therefore, much of what you learn in one of those versions will apply to the other.

See Should I use Python 2 or Python 3 for my development activity?. Also visit some of the links from that page.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.