+ 5

pls explain this python code !!!!!!

it is a translator program , can anyone explain me how the letters are replaced by the for loop and if statement ?? def trans(ph): translation = "" for letter in ph: if letter in "AEIOUaeiou": translation = translation + "N" else: translation = translation + letter return translation print(trans(input("enter a word : "))) Expecting your answer :) Thank you

12th Nov 2019, 4:18 PM
Naveenkumar v
10 Antworten
+ 17
It's not as complicated as it looks like. The input string is iterated in a for loop. All vowels will be replaced by *N*, all the rest will keep as input. enter a word : aeiouAEIOU # input NNNNNNNNNN # output enter a word : bcdfghBCDFGH # input bcdfghBCDFGH #output
12th Nov 2019, 4:28 PM
Lothar
Lothar - avatar
+ 11
Naveen Kumar, *translation* does nothing special, it's just the name of a (string) varible where the output string is composed by using concatenation.
12th Nov 2019, 4:52 PM
Lothar
Lothar - avatar
+ 7
Naveen Kumar, i forgot to mention, that there is a real function in python (translate()) that can do a translation with characters and a predefined dict. Here a sample : https://code.sololearn.com/cUo8IKPfns84/?ref=app
12th Nov 2019, 6:20 PM
Lothar
Lothar - avatar
+ 3
Thanks you Lothar
12th Nov 2019, 4:30 PM
Naveenkumar v
+ 2
@lothar can you pls explain me this line of code sir? if letter in "AEIOUaeiou": translation = translation + "N" else: translation = translation + letter how translation works!?
12th Nov 2019, 4:33 PM
Naveenkumar v
+ 2
Lothar I got it , Thanks you :)
12th Nov 2019, 4:54 PM
Naveenkumar v
+ 2
Naveen, think of translation as a building block variable. Its value starts as the empty string "" and each translation = translation + ? adds the defined letter the string (building block). Hope that helps!
12th Nov 2019, 9:44 PM
Stan Berger
Stan Berger - avatar
+ 2
Stan Berger now I understand , thanks , stan
13th Nov 2019, 1:34 AM
Naveenkumar v
+ 1
to the string
12th Nov 2019, 9:45 PM
Stan Berger
Stan Berger - avatar
+ 1
Thank you python programmer
13th Nov 2019, 11:25 PM
Umar Farouq Gimba
Umar Farouq Gimba - avatar