+ 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
10 Respuestas
+ 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
+ 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.
+ 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
+ 3
Thanks you Lothar
+ 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!?
+ 2
Lothar I got it , Thanks you :)
+ 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!
+ 2
Stan Berger now I understand , thanks , stan
+ 1
to the string
+ 1
Thank you python programmer