[Tutor] Newbie Question: Define Funcations
Sean 'Shaleh' Perry
shalehperry@attbi.com
Fri, 17 May 2002 19:56:32 -0700 (PDT)
>
> The thing is that spam hasn't been defined in the place where you call
> printtwice():
>
>>>> def printtwice(bruce):
> ... print bruce, bruce
> ...
>>>> printtwice(spam)
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> NameError: name 'spam' is not defined
>>>> spam = "Hello world" # Now give a value to spam
>>>> printtwice(spam)
> Hello world Hello world
>>>>
>
> When you call the function printtwice, the Python interpreter tries to
> find the value of the variable spam to send as an argument to the
> function. As it hasn't been given a value, there is an error message.
>
and printtwice('spam') will print the word spam twice. Note the quotes. This
makes spam into a string and not a variable name.