Popular Posts

Friday, August 28, 2020

Python Beginner: Build a Translator

 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! 😊

1 comment:

  1. If you verify the odds chart, blackjack considered one of the|is amongst the|is probably considered one of the} casino table video games with greatest odds. You should develop good strategies and ways to make the best of this recreation. There are completely different recreation variations, however all variants have the identical odds. There 온라인 카지노 are completely different “Nerd Voices for Nerdbot states that each variation of roulette is exclusive.” Each variant has a special house edge. To narrow down which variation has the best odds, we take a look at|have a glance at} the RTP.

    ReplyDelete

Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...