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