[Tutor] why do i get None as output

Andre Engels andreengels at gmail.com
Mon Sep 6 09:24:21 CEST 2010


On Mon, Sep 6, 2010 at 8:34 AM, Roelof Wobben <rwobben at hotmail.com> wrote:
> Hello,
>
> I have this programm:
>
> def encapsulate(val, seq):
>     if type(seq) == type(""):
>         return str(val)
>     if type(seq) == type([]):
>         return [val]
>     return (val,)
>
> def insert_in_middle(val, seq):
>     middle = len(seq)/2
>     return seq[:middle] + encapsulate(val, seq) + seq[middle:]
>
> def make_empty(seq):
>     """
>       >>> make_empty([1, 2, 3, 4])
>       []
>       >>> make_empty(('a', 'b', 'c'))
>       ()
>       >>> make_empty("No, not me!")
>       ''
>     """
>     word2=""
>     teller=0
>     if type(seq) == type([]):
>         teller=0
>         while teller < len(seq):
>             seq[teller]=""
>             teller = teller + 1
>     elif type(seq) == type(()):
>         tup2 = list (seq)
>         while teller > tup2.len():
>             tup2[teller]=""
>             teller = teller + 1
>         seq = tuple(tup2)
>     else:
>         seq = ""
>
> test = make_empty([1, 2, 3, 4])
> print test
>
> But now I get None as output instead of []
>
> Can anyone explain why that happens ?

test = make_empty([1, 2, 3, 4]) makes test equal to the return value
of make_empty. But make_empty does not return anything, and in that
case its return value is made equal to empty. Compare:

def f(x):
    x = x + 1

def g(x):
    x = x + 1
    return x

def h(x):
    return x +1

print f(1)
>> None

print g(1)
>> 2

print h(1)
>> 2


-- 
André Engels, andreengels at gmail.com


More information about the Tutor mailing list