Python Forum
Convert a sentence to pig latin
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert a sentence to pig latin
#1
I am writing a function that will convert a sentence to pig latin. To do this, I convert all the letters to uppercase, remove the first letter and place it at the end of the word, and then add "AY" to the end of the word. So, "Stop and smell the roses." will become "TOPSAY NDAAY MELLSAY HEAY OSESRAY.” I can't seem to get my code to work though, it only prints out "OSESRAY". How can I fix my program?

def pigLatin():
    user_string = input('Enter a string: ')
    user_string = user_string.upper()
    ay = 'AY'
    user_string = user_string.split()

    for item in user_string:
        result_pig = item[1:(len(user_string))] + item[0] + ay + ' '
    print(result_pig)

Hello everyone I was able to fix it
Reply
#2
Pig Latin for Stop is Opstay. If a word does not start with a consonant the start of the word is left unchanged and 'way' or 'yay' is added to the end. "I" becomes "Iway" or "Iyay". You should also maintain the case of the first letter.

This complicates the translation a bit. Not only do you use different solutions based on if the word starts with a consonant, but you need to look for consonant clusters and also fix the case.
Reply
#3
I think it remains simple - Check each character until you reach a vowel, then move the consonants to the end and add "ay". If no consonants, add yay (or ay as I learned it).
Reply
#4
Certainly harder than taking the first letter, move to the end and append 'ay'. Not hard by any means, but relatively it may be 5 times harder?
Reply
#5
Hello everyone, this was a homework assisgnment and I translated the sentence the way my teacher asked me to. I do realize it is more complicated than the way I was told to do it. But I was able to figure out the problem. Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  write split words of sentence to file bluefrog 1 2,990 Aug-27-2018, 01:28 AM
Last Post: micseydel
  Reverse string sentence with for loop? BillGates 3 6,109 May-02-2017, 04:15 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020