Python Code leave some comments explaining the code Thank yo
Python Code, leave some comments explaining the code. Thank you!!
Write a program in python that lets the user enter in some English text, then converts the text to Pig-Latin.
To review, Pig-Latin takes the first letter of a word, puts it at the en and appends “ay”. The only exception is if the first letter is a vowel, in which case we keep it as is and append “hay” to the end. E.g. “hello”-> “ellohay”, and “image> “imagehay” It will be useful to define a list or tupple at the top called VOWELS.
This way you can check if a letter x is a vowel with the expression x in VOWELSIt’s tricky for us to deal with punctuation and numbers with what we know so far, so instead, ask the user to enter only words and spaces.
You can convert their input from a string to a list of strings by calling split on the string:
\"My name is John Smith\".split(\" \") -> [\"My\", \"name\", \"is\", \"John\", \"Smith\"]
Using this list, you can go through each word and convert is to Pig-Latin. Also, to get a word except for the first letter, you can use word[1:].
Hints: It will make your life much easier–and your code much better–if you separate tasks into functions, e.g. have a function that converts one word to Pig-Latin rather than putting it into your main program code.
Solution
VOWELS = (\"a\", \"e\", \"i\", \"o\", \"u\", \"A\", \"E\", \"I\", \"O\", \"U\")
print \"\ Conversion of English text to Pig Latin.\"
print \"\ Please Note that: Punctuations and numbers are not yet supported.\"
print \"\ Please Enter only letters and spaces.\"
def conv_consonant_start(Sentance):
Latin_word_pig= Sentence[0]+Sentence[1]+ \"ay\"
return Latin_word_pig
def conv_vowel_start(Sentence):
Latin_word_pig = Sentence + \"hay\"
return Latin_word_pig
English = raw_input(\"Please Enter your sentence that you want convert: \")
English_Sentence = English.split(\" \")
Latin_word_pig = []
for j in English_Sentence
first_letter = j[0]
vowel = False
if first_letter in VOWELS:
vowel = True
if vowel == True:
pig_latin_words.append(conv_vowel_start(j))
else:
pig_latin_words.append(conv_consonant_start(j))
print \"\ \\tEnglish:\", English
print \"\\tJoining the PIG-LATIN\", \" \".join(Latin_word_pig)

