Learning Reflection Pig Latin Translator

MohMoh
3 min read

Introduction

Today, I decided to apply my Python knowledge to tackle the first project in Impractical Python Projects: building a Pig Latin translator. This challenge was a fun way to practice string manipulation, loops, and conditional logic. Below, I’ve shared my solution and compared it to the book’s version, reflecting on the differences and lessons learned.


My Solution

Here's the code I wrote for the Pig Latin translator:

vowels = ["a", "e", "i", "o", "u"]

while True:
    word = input("Please type a word to translate to pig latin (or type 'q' to exit):\n").lower()

    if word == "q":
        print("Goodbye")
        break

    char_list = list(word)

    if char_list[0] in vowels:
        print(f"{word}yay")
    else:
        f_letter = char_list.pop(0)
        char_list.append(f_letter)
        print(f"{''.join(char_list)}ay")

How It Works:

  • I defined a list of vowels and used it to determine if the first letter was a vowel.

  • If the word started with a vowel, I added "yay" to the end.

  • If it started with a consonant, I moved the first letter to the end and added "ay".

  • The program allows users to input multiple words, exiting with 'q'.


Book Solution

Here’s the solution provided in the book:

"""Turn a word into its Pig Latin equivalent."""
import sys

VOWELS = 'aeiouy'

while True:
    word = input("Type a word and get its Pig Latin translation: ")

    if word[0] in VOWELS:
        pig_Latin = word + 'way'
    else:
        pig_Latin = word[1:] + word[0] + 'ay'

    print()
    print("{}".format(pig_Latin), file=sys.stderr)

    try_again = input("\n\nTry again? (Press Enter else n to stop)\n")
    if try_again.lower() == "n":
        sys.exit()

🔍 How It Works:

  • Uses a string of vowels ('aeiouy') for simpler membership checks.

  • Uses slicing (word[1:], word[0]) for rearranging characters.

  • Adds "way" if the word starts with a vowel.

  • Uses the sys module to cleanly exit and print to stderr.


Reflection: Differences and Insights

1. Data Structure for Vowels

  • Mine: List (["a", "e", "i", "o", "u"]) – intuitive for me while practicing lists.

  • Book: String ('aeiouy') – more concise and efficient.

  • Takeaway: Strings work better for single-character membership checks.

2. Handling Translation Logic

  • Mine: Converted the word to a list to manipulate characters.

  • Book: Used slicing – more Pythonic and efficient.

  • Takeaway: Avoid unnecessary data structure changes when strings suffice.

3. Program Interaction

  • Mine: Exit via typing "q".

  • Book: Offers a retry prompt, exits cleanly with sys.exit().

  • Takeaway: Adding prompts enhances UX and polish.


Lessons Learned

  • Keep it simple: The book’s version is shorter and more elegant.

  • User experience matters: A retry prompt improves flow.

  • Explore new tools: Using modules like sys opens up new possibilities.

This project was a great way to practice problem-solving and reflect on different styles of coding. Going forward, I’ll aim for more concise and readable solutions.

Closing Thoughts

Tackling the Pig Latin translator was a rewarding hands-on exercise. Comparing my approach with the book’s gave me useful insights and highlighted areas where I can improve. I'm excited to continue working through more projects and leveling up my Python skills.


Thanks for reading, and let’s see what the tide brings tomorrow!


Join Me on My Journey

If you’d like to follow along, I’ll be sharing more about my learning process, projects, and insights here and on my GitHub repository. Feel free to connect — I’d love to hear about your coding journey too!

0
Subscribe to my newsletter

Read articles from Moh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Moh
Moh

Python enthusiast and data professional documenting my coding journey as it develops on CodingTides.com