From f8e6619a1258e7bfa296b8c0556e5e84e87da2a3 Mon Sep 17 00:00:00 2001 From: lolabird Date: Sun, 19 Nov 2023 19:49:16 -0700 Subject: [PATCH] Improve translation with plurals and punctuation --- thinkcspy/Chapter-12-Exercises/Problem5.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/thinkcspy/Chapter-12-Exercises/Problem5.py b/thinkcspy/Chapter-12-Exercises/Problem5.py index 3618531..9c08403 100644 --- a/thinkcspy/Chapter-12-Exercises/Problem5.py +++ b/thinkcspy/Chapter-12-Exercises/Problem5.py @@ -19,13 +19,15 @@ def translator(english): pirate['is'] = 'be' pirate['man'] = 'matey' - englishList = english.split() + [" "] + englishList = english.split() + [' '] for i in range(len(englishList) - 1): word = englishList[i] word = word.lower() newWord = "" lengthy = len(newWord) - 1 + lengthier = len(word) - 1 + lengthiest = len(word) - 2 for char in word: if char.isalpha(): @@ -35,7 +37,18 @@ def translator(english): newWord = newWord[0:lengthy] if newWord in pirate: - englishList[i] = pirate[newWord] + if newWord != word: + if word[lengthier] == 's' or word[lengthiest] == 's': + englishList[i] = pirate[newWord] + 's' + + if not word[lengthiest].isalpha(): + englishList[i] = pirate[newWord] + word[lengthiest] + + elif not word[lengthiest].isalpha(): + englishList[i] = pirate[newWord] + word[lengthiest] + else: + englishList[i] = pirate[newWord] + translation = " ".join(englishList)