-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsession_6.py
42 lines (28 loc) · 1.38 KB
/
session_6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#Pig Latin translator
#English is translated to Pig Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’. “The quick brown fox” becomes “Hetay uickqay rownbay oxfay”.
#Flip the word into pig latin
def translate_word(word):
vowels = 'aeiou'
# go over list slicing
# We can access characters in a string as a list.
# [0] => get 0th index. (1st character in the string)
# lists can be sliced easily in python
# [0:2] => get the first 2 character (0 and 1 index - index 2 is NOT included)
# [1:] => get the 1st index and everything after
lower_case_word = word.lower()
first_letter = lower_case_word.lower()[0]
remaining_letters = lower_case_word.lower()[1:]
if first_letter in vowels:
return word + 'yay'
else:
return ''.join(remaining_letters) + first_letter + 'ay'
#Break apart the sentence into words, then combine new words back together again
def translate_sentence(sentence):
translated_sentence = ""
for word in sentence.split(" "):
translated_sentence += translate_word(word) + " "
return translated_sentence.strip()
#Call the functions, and return the value
print(translate_sentence('The quick brown fox'))
#Challenge
# Take sentence(s) from user, ensure no punctuation. Use a loop(s) to take in many sentences