← Back to Blog

FOR IF Mini Project — Pig Latin

The problem...

You've seen encryption. You've seen formatting. Now something different.

Pig Latin is a word game. Simple rules. Surprising output. And a perfect job for everything you've learned so far.

The rules

  • If the word starts with a vowel — add "yay" at the end
  • If the word starts with a consonant — move the first letter to the end and add "ay"
  • If the word is only one character — skip it
# "apple" → "appleyay"
# "bull"  → "ullbay"
# "horn"  → "ornhay"
# "a"     → skipped

What you need to know first

String slicing — you've seen it in Basics:

word = "Bull"
print(word[1:])    # ull — everything from index 1 onward
print(word[0])     # B — first character

Your mission

Ask the user for a sentence. Loop through each word. Skip words that are too short. Translate the rest.

The solution

sentence = input("Enter a sentence: ").lower()
words = sentence.split()
vowels = "aeiou"
result = ""

for word in words:
    if len(word) <= 1:
        continue    # skip — too short to translate

    if word[0] in vowels:
        result += word + "yay "
    else:
        result += word[1:] + word[0] + "ay "

print(result.strip())

Test it

# Enter a sentence: bull horn red
# ullbay ornhay edray

# Enter a sentence: a bull and a horn
# ullbay andyay ornhay
# "a" appears twice — both skipped by continue

What's really happening

The loop visits every word. continue skips the ones that are too short — no translation, no output, straight to the next word. The rest get translated and added to the result.

Go further

  • Handle words that start with multiple consonants — "string" → "ingstray"
  • Use break to stop translating after the first 5 words
  • What happens with numbers in the sentence? Can you handle them with continue?

What you should understand now

  • continue skips the current iteration — the loop keeps going
  • String slicing — word[1:] gives everything from index 1 onward
  • in checks membership — word[0] in vowels checks if the first letter is a vowel
  • .split() separates a sentence into individual words
[ login to bookmark ] // copied! 22 views · 1 min
// resources
Exercise pig_latin.py
← prev FOR IF Mini Project — Password Strength Checker next → Common PASS/CONTINUE/BREAK Mistakes
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.