lundi 23 mars 2020

string.lower() in an if loop doesn't lower case the string in Python?

def translate(phrase):
    translation= ""
    for letter in phrase:
        if letter.lower() in "aeiou":
            if letter.isupper():
                translation = translation + "G"
            else:
                translation = translation + "g"

        else:
            translation = translation + letter
    return translation

print(translate(input("Enter a phrase: ")))

(This is a program to change vowels to the letter 'g' or 'G' depending if it is upper or lower case)

Why does this work? Since there is a letter.lower() shouldn't it transform that letter to a lowercase one? that way the next if statement ("if letter.isupper():") would never be true...

I'm confused because I only know C and I'm trying to learn Python. In C if I did that it would transform the letter variable to a lower case one so the next if statement would never be true. I'm guessing that functions inside if statements in Python don't change/alter the variables inside those same fucntions...? But then again that wouldn't really make sense to me... What am I thinking wrong?

Aucun commentaire:

Enregistrer un commentaire