def translate(phrase):
translation = ""
for letter in phrase:
if letter.lower() in "aeiouy":
if letter.upper():
translation = translation + "G"
else:
translation = translation + "g"
else:
translation = translation + letter
return translation
print(translate(input("Enter A Phrase to Translate: ")))
I'm just learning python and following a tutorial and I don't understand this code. So this is a basic translator that makes all vowels "G", the for loop picks every letter in a phrase that the user enters and checks if it is a vowel, what I don't get is after it finds the vowels, it adds g to the translation, how does this work, if your trying to replace the vowel why does adding g to the entire thing translate it?
The key is here:
if letter.lower() in "aeiouy":
if letter.upper():
translation = translation + "G"
else:
translation = translation + "g"
else:
translation = translation + letter
If the letter is a vowel, then it adds a g instead of the supposed vowel (translation = translation + letter
won't be executed).
Also, since letter.upper()
doesn't check if it's lowercase (it just turns the character into uppercase), translation = translation + "g"
will never be reached- maybe replace letter.upper()
with letter == letter.upper()
?