python_exercises/thinkcspy/Chapter-12-Exercises/Problem5.py

59 lines
1.7 KiB
Python
Raw Normal View History

2023-11-19 19:13:34 -07:00
def translator(english):
pirate = {}
pirate['sir'] = 'matey'
pirate['hotel'] = 'fleabag inn'
pirate['student'] = 'swabbie'
pirate['boy'] = 'matey'
pirate['restaurant'] = 'galley'
pirate['hello'] = 'avast'
pirate['madam'] = 'proud beauty'
pirate['professor'] = 'foul blaggart'
pirate['your'] = 'yer'
pirate['excuse'] = 'arr'
pirate['are'] = 'be'
pirate['lawyer'] = 'foul blaggart'
pirate['the'] = 'th\''
pirate['restroom'] = 'head'
pirate['my'] = 'me'
pirate['is'] = 'be'
pirate['man'] = 'matey'
englishList = english.split() + [' ']
2023-11-19 19:13:34 -07:00
for i in range(len(englishList) - 1):
word = englishList[i]
word = word.lower()
newWord = ""
2023-11-19 19:36:11 -07:00
lengthy = len(newWord) - 1
lengthier = len(word) - 1
lengthiest = len(word) - 2
2023-11-19 19:13:34 -07:00
for char in word:
if char.isalpha():
newWord += char
2023-11-19 19:36:11 -07:00
if newWord[lengthy] == 's':
newWord = newWord[0:lengthy]
2023-11-19 19:13:34 -07:00
if newWord in pirate:
if newWord != word:
if word[lengthier] == 's' or word[lengthiest] == 's':
englishList[i] = pirate[newWord] + 's'
2023-11-19 20:17:46 -07:00
if not word[lengthier].isalpha():
englishList[i] = pirate[newWord] + 's' + word[lengthier]
2023-11-19 20:17:46 -07:00
elif not word[lengthier].isalpha():
englishList[i] = pirate[newWord] + word[lengthier]
else:
englishList[i] = pirate[newWord]
2023-11-19 19:13:34 -07:00
translation = " ".join(englishList)
return translation
print(translator(input("What would you like to say?")))