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 solution, 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 of the word was a vowel.
  • If the word started with a vowel, I added “yay” to the end.
  • If the word started with a consonant, I moved the first letter to the end and added “ay”.
  • The program allows the user to repeatedly input words and exit by typing “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:

  • The book uses a string ('aeiouy') for vowels instead of a list.
  • It directly checks the first character of the word and appends “way” if it’s a vowel.
  • If the word starts with a consonant, it uses slicing to remove the first character and append it to the end along with “ay”.
  • The sys module is used for exiting the program and printing output to the standard error stream.

Reflection: Differences and Insights

  1. Data Structure for Vowels:

    • My Approach: I used a list (["a", "e", "i", "o", "u"]), which felt intuitive as I’ve been working with lists recently.
    • Book’s Approach: The book uses a string ('aeiouy'), which is more concise and avoids the need for creating a separate list.
    • Takeaway: Using a string is simpler when checking membership with in for single characters.
  2. Handling the Translation Logic:

    • My Approach: I converted the word into a list of characters to manipulate it.
    • Book’s Approach: The book uses slicing (word[1:] and word[0]) to handle character rearrangement.
    • Takeaway: Slicing is more Pythonic and efficient for operations on strings. My approach involved unnecessary conversion to a list.
  3. Program Interaction:

    • My Approach: The program continuously loops and exits when the user types “q”.
    • Book’s Approach: It prompts the user to confirm if they want to try again and exits cleanly using sys.exit().
    • Takeaway: Including a confirmation prompt makes the program feel more polished and user-friendly.

Lessons Learned

  • Simplicity is key. The book’s use of slicing and a string for vowels resulted in cleaner and more efficient code.
  • Adding user-friendly touches, like a retry prompt, can improve the overall experience.
  • Exploring alternatives, such as using sys for program termination and error handling, expands my toolkit for future projects.

This project was a great exercise in problem-solving and comparing different coding styles. Moving forward, I’ll aim to write more concise and Pythonic code, keeping readability and efficiency in mind.


Closing Thoughts

Tackling the Pig Latin translator was a rewarding way to apply what I’ve learned so far. Comparing my approach with the book’s solution has given me new insights and highlighted areas for growth. Next, I’m excited to dive into more projects and continue refining my 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!