<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
<BR>&nbsp;<BR>
&gt; Date: Wed, 25 Aug 2010 12:27:39 +0200<BR>&gt; From: cwitts@compuscan.co.za<BR>&gt; To: tutor@python.org<BR>&gt; Subject: Re: [Tutor] why does this fail<BR>&gt; <BR>&gt; On 25/08/2010 12:00, Roelof Wobben wrote:<BR>&gt; &gt; Hello,<BR>&gt; &gt;<BR>&gt; &gt; I have this programm :<BR>&gt; &gt;<BR>&gt; &gt; def remove_letter(letter, strng):<BR>&gt; &gt; """<BR>&gt; &gt; &gt;&gt;&gt; remove_letter('a', 'apple')<BR>&gt; &gt; 'pple'<BR>&gt; &gt; &gt;&gt;&gt; remove_letter('a', 'banana')<BR>&gt; &gt; 'bnn'<BR>&gt; &gt; &gt;&gt;&gt; remove_letter('z', 'banana')<BR>&gt; &gt; 'banana'<BR>&gt; &gt; &gt;&gt;&gt; remove_letter('i', 'Mississippi')<BR>&gt; &gt; 'Msssspp'<BR>&gt; &gt; """<BR>&gt; &gt; antwoord=""<BR>&gt; &gt; for letter in strng:<BR>&gt; &gt; print letter, strng<BR>&gt; &gt; if letter in strng:<BR>&gt; &gt; print "false"<BR>&gt; &gt; else:<BR>&gt; &gt; print "true"<BR>&gt; &gt; return antwoord<BR>&gt; &gt;<BR>&gt; &gt; x=remove_letter('a', 'apple')<BR>&gt; &gt; print x<BR>&gt; &gt;<BR>&gt; &gt; But now everything is false even a in apple.<BR>&gt; &gt;<BR>&gt; &gt; What is here wrong ?<BR>&gt; &gt;<BR>&gt; &gt; Roelof<BR>&gt; &gt;<BR>&gt; &gt;<BR>&gt; &gt; _______________________________________________<BR>&gt; &gt; Tutor maillist - Tutor@python.org<BR>&gt; &gt; To unsubscribe or change subscription options:<BR>&gt; &gt; http://mail.python.org/mailman/listinfo/tutor<BR>&gt; &gt; <BR>&gt; <BR>&gt; You're rebinding the variable `letter`.<BR>&gt; It is an input variable for your function but then you use it as the <BR>&gt; character store while iterating through your string variable `strng`.<BR>&gt; <BR>&gt; What your control should look like would be more like<BR>&gt; <BR>&gt; for character in strng:<BR>&gt; if letter == character:<BR>&gt; print 'false' # this should be true, it is a match just like in <BR>&gt; your example<BR>&gt; else:<BR>&gt; print 'true'<BR>&gt; <BR>&gt; I'm assuming this function is just for learning purposes because there's <BR>&gt; a built-in string function you can use called replace and you'd use it <BR>&gt; as such `'apple'.replace('a', '')`.<BR>&gt; <BR>&gt; PS: Once you've gotten it to work convert it to a list comprehension, <BR>&gt; they are incredibly useful and a great tool.<BR>&gt; <BR>&gt; -- <BR>&gt; Kind Regards,<BR>&gt; Christian Witts<BR>&gt; <BR>&gt; <BR>&gt; _______________________________________________<BR>&gt; Tutor maillist - Tutor@python.org<BR>&gt; To unsubscribe or change subscription options:<BR>&gt; <A href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</A><BR><BR>
Hello Christian.<BR>&nbsp;<BR>It's for learning purposed but I forget that de module string has built in functions.<BR>Thank you for remainding it to me.<BR>&nbsp;<BR>list comprehistion is q few chapters later in the book so I don't try yet.<BR>&nbsp;<BR>Roelof<BR>                                               </body>
</html>