문제

I am writing a small script that goes through a string and finds a given character and replaces it with another.

def scrambler(string):
    sen = ''
    for c in string:
        if c.lower == 'k':
            sen += 'm'
        elif c.lower == 'o':
            sen += 'q'
        elif c.lower == 'e':
            sen += 'g'
        else:
            sen += c
    return sen

print scrambler('koe')

As you can see if the letter is 'k' then 'm' should be added to sen. For some reason the condition is not being met but I am not sure why.

도움이 되었습니까?

해결책

You are comparing the function object c.lower with a character constant: you need to invoke the function, instead:

def scrambler(string):
    sen = ''
    for c in string:
        if c.lower() == 'k':
            sen += 'm'
        elif c.lower() == 'o':
            sen += 'q'
        elif c.lower() == 'e':
            sen += 'g'
        else:
            sen += c
    return sen

print scrambler('koe')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top