Error in creating function spam()


#1


https://www.codecademy.com/courses/python-beginner-c7VZg/1/1?curriculum_id=4f89dab3d788890003000096#

Oops, try again. Make sure your function prints "Eggs!" to the console!

I can't understand whats wrong with the code


def spam():
    """Prints 'Eggs!'"""
    print "Eggs!"

https://www.codecademy.com/courses/python-beginner-c7VZg/1/1?curriculum_id=4f89dab3d788890003000096#


#2

Once we define / make our function, To use that function we need to call it.

Like this..

suppose a function is here..

def ACoolFunction():
    print "some cool thing"

Now, How to call this function ?


simple, name of function and (),with parameter(s) (if it has any)

ACoolFunction()


Note:
In ahead of section, You will come across function(s),That returns a value and does not just prints things, so you may need to save the call-value in some variable.


#3

How can the console print "Eggs!" if my function is "spam ()"? . I don´t understand that, please help. I understand, perfectly, your example and the codecademy's example...


#4

hello @nayelilv

function is nothing but a wrapper with a name on it,
It wraps code lines that is going to repeated in a big program.
When we call the function by its name and with the data(parameter/argument) its needs(you will learn more about function that needs data in next exercises).
The function simply runs the code inside of it.


Like in this example when we call the function, it simply checks what lines of code there(inside of function) and runs it, as you can see function have one line of code that prints a string so we have string printed in console.


#5

def spam():

prints something

print "Eggs!"

spam()

But it doesnt work why?


#6

def spam():

prints something

print "Eggs!"
return

Define the spam function above this line.

spam()

this does not work as well


#7

hi @bitninja23259
Make sure you indented the code lines inside the function carefully,
like this...

def some():
    #somecode

Can you see I made 4-spacebars space there ?
to call this function,I'd do this.

some()

#8

I already solved it, but there are defined only very narrowly defined
solutions ..if you print no eggs but somethiing else it does not work...but
point in learning should be so that you should understand process not that
ther eis only printed eggs....
Thanks you anyway:)


#9

Oh yeas CC is quite strict towards instruction following and in this exercise it mentioned what to print ("Eggs!"), so it won't work for any other string but in general it does work!


#10

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