[Tutor] why does this fail

Alan Gauld alan.gauld at btinternet.com
Wed Aug 25 20:29:49 CEST 2010


"Roelof Wobben" <rwobben at hotmail.com> wrote

######################
def remove_letter(letter, strng):
    antwoord=""
    for letter in strng:
        print letter, strng
        if letter in strng:
            print "false"
        else:
            print "true"
    return antwoord
######################

Several issues:

1) You never use antwoord so it alweays returns ""
2) you print false when the letter IS in the string
3) You use letter as an input parameter but then overwrite it in the 
for loop.
4) your if test would be better written as

print (letter in string)

(The parens aren't necessary but I think make it clearer that
we are printing the evaluation of an expression not just a string with
missing quotes...)

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list