[Tutor] why do i get None as output

Roelof Wobben rwobben at hotmail.com
Mon Sep 6 09:41:18 CEST 2010



 

> To: tutor at python.org
> From: alan.gauld at btinternet.com
> Date: Mon, 6 Sep 2010 08:27:31 +0100
> Subject: Re: [Tutor] why do i get None as output
> 
> 
> "Roelof Wobben" <rwobben at hotmail.com> wrote 
> 
> def make_empty(seq):
> 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])
> 
> But now I get None as output instead of []
> 
> 
> Because None is the default return value from a function.
> If you do not return a value (which you don;t in this case) then 
> Python automatically returns None.
> 
> You need to return something from your make_empty function.
> 
> Also, if all you want to do is return an empty version of 
> whatever has been passed in there are much easier 
> ways of doing it! And in fact, a list of empty strings is 
> not the same as an empty list...
> 
> 
> HTH
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

 

Oke, 

 

I put a return seq in the programm and it looks now like this :

 

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!")
      ''
    """
    if type(seq) == type([]):
        seq = []
    elif type(seq) == type(()):
        seq=()
    else:
        seq = ""
    return seq

 

if __name__ == "__main__":
    import doctest
    doctest.testmod()

 

This works but I don't think its what the exercise means :

 



Create a module named seqtools.py. Add the functions encapsulate and insert_in_middle from the chapter. Add doctests which test that these two functions work as intended with all three sequence types.

Add each of the following functions to seqtools.py:

def make_empty(seq):
    """
      >>> make_empty([1, 2, 3, 4])
      []
      >>> make_empty(('a', 'b', 'c'))
      ()
      >>> make_empty("No, not me!")
      ''
    """

So i think I have to use encapsulate and insert_in_middle. And I don't use it.

 

Roelof


 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100906/7c12ce6c/attachment-0001.html>


More information about the Tutor mailing list