<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
<BR> <BR>
> To: tutor@python.org<BR>> From: alan.gauld@btinternet.com<BR>> Date: Mon, 6 Sep 2010 08:27:31 +0100<BR>> Subject: Re: [Tutor] why do i get None as output<BR>> <BR>> <BR>> "Roelof Wobben" <rwobben@hotmail.com> wrote <BR>> <BR>> def make_empty(seq):<BR>> word2=""<BR>> teller=0<BR>> if type(seq) == type([]):<BR>> teller=0 <BR>> while teller < len(seq):<BR>> seq[teller]=""<BR>> teller = teller + 1 <BR>> elif type(seq) == type(()):<BR>> tup2 = list (seq)<BR>> while teller > tup2.len():<BR>> tup2[teller]=""<BR>> teller = teller + 1<BR>> seq = tuple(tup2)<BR>> else:<BR>> seq = ""<BR>> <BR>> test = make_empty([1, 2, 3, 4])<BR>> <BR>> But now I get None as output instead of []<BR>> <BR>> <BR>> Because None is the default return value from a function.<BR>> If you do not return a value (which you don;t in this case) then <BR>> Python automatically returns None.<BR>> <BR>> You need to return something from your make_empty function.<BR>> <BR>> Also, if all you want to do is return an empty version of <BR>> whatever has been passed in there are much easier <BR>> ways of doing it! And in fact, a list of empty strings is <BR>> not the same as an empty list...<BR>> <BR>> <BR>> HTH<BR>> <BR>> -- <BR>> Alan Gauld<BR>> Author of the Learn to Program web site<BR>> http://www.alan-g.me.uk/<BR>> <BR>> <BR>> _______________________________________________<BR>> Tutor maillist - Tutor@python.org<BR>> To unsubscribe or change subscription options:<BR>> <A href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</A><BR>
<BR>
Oke, <BR>
<BR>
I put a return seq in the programm and it looks now like this :<BR>
<BR>
def encapsulate(val, seq):<BR> if type(seq) == type(""):<BR> return str(val)<BR> if type(seq) == type([]):<BR> return [val]<BR> return (val,)<BR>
<BR>
def insert_in_middle(val, seq):<BR> middle = len(seq)/2<BR> return seq[:middle] + encapsulate(val, seq) + seq[middle:]<BR>
<BR>
def make_empty(seq):<BR> """<BR> >>> make_empty([1, 2, 3, 4])<BR> []<BR> >>> make_empty(('a', 'b', 'c'))<BR> ()<BR> >>> make_empty("No, not me!")<BR> ''<BR> """<BR> if type(seq) == type([]):<BR> seq = []<BR> elif type(seq) == type(()):<BR> seq=()<BR> else:<BR> seq = ""<BR> return seq<BR>
<BR>
if __name__ == "__main__":<BR> import doctest<BR> doctest.testmod()<BR>
<BR>
This works but I don't think its what the exercise means :<BR>
<BR>
<OL>
<LI>
<P class=first>Create a module named <TT class="docutils literal"><SPAN class=pre>seqtools.py</SPAN></TT>. Add the functions <TT class="docutils literal"><SPAN class=pre>encapsulate</SPAN></TT> and <TT class="docutils literal"><SPAN class=pre>insert_in_middle</SPAN></TT> from the chapter. Add doctests which test that these two functions work as intended with all three sequence types.</P></LI>
<LI>
<P class=first>Add each of the following functions to <TT class="docutils literal"><SPAN class=pre>seqtools.py</SPAN></TT>:</P>
<DIV class=highlight-python>
<DIV class=highlight><PRE><SPAN class=k>def</SPAN> <SPAN class=nf>make_empty</SPAN><SPAN class=p>(</SPAN><SPAN class=n>seq</SPAN><SPAN class=p>):</SPAN>
<SPAN class=sd>"""</SPAN>
<SPAN class=sd> >>> make_empty([1, 2, 3, 4])</SPAN>
<SPAN class=sd> []</SPAN>
<SPAN class=sd> >>> make_empty(('a', 'b', 'c'))</SPAN>
<SPAN class=sd> ()</SPAN>
<SPAN class=sd> >>> make_empty("No, not me!")</SPAN>
<SPAN class=sd> ''</SPAN>
<SPAN class=sd> """</SPAN>
</PRE></DIV></DIV></LI></OL>
So i think I have to use encapsulate and insert_in_middle. And I don't use it.<BR>
<BR>
Roelof<BR>
<BR> <BR>                                            </body>
</html>