-3

I can't seem to fully understand this code. Help will be much appreciated. This is a code where when I input a phrase or a word, the result will give me the same thing except for every vowel changed into a "g"

def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter in "AEIOUaeiou":
            translation += "g"
        else:
            translation += letter
    return translation

I can figure everything else out except, the part where translation +="g" I don't get how that will switch every vowel into a "g" So, I would love for someone to walk me through this code, step by step please. Thank you.

Kévin Barré
  • 330
  • 1
  • 4
  • 11
A.Lee
  • 3
  • 1
  • 3
    This is loading the translation into a new variable called `translation`. By looping through every character, it keeps adding either the "g" or the consonant, so the final result is the one you describe. – fedorqui Aug 10 '18 at 11:28
  • 3
    It adds each letter to `translation`, except if that letter is present in `"AEIOUaeiou"`, in which case it adds a `"g"` instead. – khelwood Aug 10 '18 at 11:28
  • For future reference, running your code through a debugger is usually a lot clearer and more efficient than asking how it works on SO. –  Aug 10 '18 at 11:33

1 Answers1

0

I would recommend you check this post on the use of 'in' statements. In general in can be applied to any kind of iterable, in the case of strings, s1 in s2 will return True if s1 is a sub string of s2. Because the loop only iterates over single characters, it will add a 'g' to the result, every time the character is a sub string of 'AEIOUaeiou'.

Chris
  • 710
  • 7
  • 15