Question about whitespace


Replace this line with your code.


What is your question?


The example given in the whitespace lesson is:

def spam():
... eggs = 12
... return eggs

print spam()

This returns a SyntaxError: Invalid Syntax in both IDLE and the command line.



What is idle? When you let it sit somewhere for too long? When I typed mine it passed.
4 space bars then the letter "e"
4 space bars then the letter "r"
What's not explained yet,I'm hoping will be later" is how do we know when to indent? Why are only the 2 middle lines indented. Also if this is about white space why is line 4 ok to be left blank?


I'm new also, so corrections are encouraged, but IDLE is the Python console(?) that comes with Python that is used to run and test small pieces of code. The example passes in codeacademy, but produces a syntax error in the two Python consoles (I hope I'm using that term correctly) I've used, which are IDLE and the command line. My guess is that it has to do with the version of Python being used? It doesn't say anywhere obvious, but I'm guessing codeacademy is teaching Python 2.x. Do you have any insights on this aquaphoenix?


The indentation is important because that is how the computer and other programmers read your code. It helps organize your code into what I like to think of hierarchies. It tells what part of the command it to be read first, which parts are meant to be grouped together, etc.

So with the exercise in question:

def spam(): #here we define a function called "spam". Anything INDENTED beneath it is a part of that function.
    eggs = 12 #here, as a part of the function, we set a variable (eggs) equal to 12
    return eggs #here we say we want the this function to return (or spit out) the value related to the variable (eggs) which is 12
#the empty space here in line 4 serves as an indicator that there are no more instructions for the above function (spam)

print spam() #here we are telling the computer to print the result from the function spam to the screen. Which would be 12, since the function spam returns or spits out the number 12. Here we have the computer just print that on the screen for you.

@jmharlock I'm not sure why it's giving you a syntax error outside of the codecademy interpreter. What does the error say specifically?

Also, instead of pressing space four times, pressing the TAB button instead does the same thing and saves you a bit of time.


Thanks for you're help erinboberin, here's a screenshot:


P.S. That is a good tip, using the tab instead of four spaces!


I'm a beginner please. What does Python understand def spam() to mean in this:
def spam():
... eggs = 12
... return eggs

print spam()


spam() is a function. Whenever you call:

def spam()

you are defining a function called spam(). This means that whatever you put inside the function will run when you call it like this:


The above command would execute whatever code is inside your function, for instance the function in your program would run:

eggs = 12
return eggs

When you call print spam() you are telling the program to print all returned values from the function to the console. In your case, calling print spam() would print the value for eggs. Since eggs = 12 the console should print 12. I hope this helps.


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