Hello, Developer!
I wish you didn't experience while understand multi-dimension lists and nested loop feature. Today, we will build a "Translator". "Translator" will get input strings or phrases from user and change target alphabets into mapped characters. For instance, user input some strings like "Apple" and we want to exchange vowels(a, e, i, o, u) into "x" from input strings.
Here is translation process example for your understanding.
Input String : Apple # Check each characters of input string and exchange vowels into "x" when it find vowels
Translated String: xpplx
Additionally, we also adding feature this translator checks "Upper" & "Lower" character and output Upper X and Lower x in case. For this feature, we can use simply matching upper or lower character vowels or we can convert each character into lower character and compare it is lower vowel and if it is vowel, once again check it is upper character, if it is upper character, exchange character into upper target character(i.e. "X") or it is lower character, exchange it into lower target character(i.e. "x")
Here, I showed full exercise codes and execution result as following.
def translate(phrase):
translation =""
for letter in phrase:
if letter in "AEIOUaeiou":
translation = translation + "x"
else:
translation = translation + letter
return translation
print(translate(input("Enter your phrase: ")))
def translate(phrase):
translation =""
for letter in phrase:
if letter in "AEIOU":
translation = translation + "X"
elif letter in "aeiou":
translation = translation + "x"
else:
translation = translation + letter
return translation
print(translate(input("Enter your phrase 2: ")))
def translate(phrase):
translation =""
for letter in phrase:
if letter.lower() in "aeiou":
if letter.isupper():
translation = translation + "X"
else:
translation = translation + "x"
else:
translation = translation + letter
return translation
print(translate(input("Enter your phrase 3: ")))
Can you see the same result on your PC with your coding? Great job! Now you can apply loop, if statements and function feature as well.
Alright, see you on next post, Developer! 😊