Home > OS >  Trying to convert a string with multiple words to Pig Latin
Trying to convert a string with multiple words to Pig Latin

Time:10-05

I'm having trouble converting the words. Ex.) I enter "hi hey hello" and I just get "ihay ihay ihay" Idk why it doesn't want to enter into the other index ranges in my for loop

Any help is appreciated!

Problem:

If a word starts with a consonant (or cluster of consonants that form one sound), move the consonant(s) to the end of the word, and add “ay” to the end

  • Example: “computer” becomes “omputercay”

If a word starts with a vowel, add “yay” on to the end of the word

  • Example: “engineering” becomes “engineeringyay”

Note: treat “y” as a vowel for this assignment

words = input("Enter word(s) to convert to Pig Latin: ").upper()
wl = words.split(). # words list

c = ("B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "X", "Z"). # consonant list
v = ("A", "E", "I", "O", "U", "Y"). # vowels list
a = "ay"
w = "way"

def piglatin(self):
    pt = []
    for i in range(len(wl)):
        if wl[i][0] in c:
            word = wl[0][1:]   wl[0][0]   a
            pt.append(word)
        else:
            word = wl[0][1:]   wl[0][0]   w
            pt.append(word)
        print(i)
    return " ".join(pt)

print(f"In Pig Latin, {words} is: {piglatin(words)}")

CodePudding user response:

You are aren't addressing the index when you are editing the words in your for loop. Instead of relying solely on indexes to select your words you could also just iterate the list directly.

I am also going to make a few other observations, such as you are supplying an argument to the piglatin function and never using it and you are relying on the global variable instead. You are using single letters for variable names, you have long tuples of single characters when a contiguous string would work just as well. For the sake of readability and clarity these practices are best avoided where possible

for example:

CONSONANTS = "BCDFGHJKLMNPQRSTVXZ" # consonant list
VOWELS = "AEIOUY" # vowels list
AY = "ay"
WAY = "way"

def piglatin(words):
    pt = []
    for word in words.split():
        if word[0] in CONSONANTS:
            word = word[1:]   word[0]   AY
        else:
            word = word[1:]   word[0]   WAY
        pt.append(word)
    return " ".join(pt)

words = input("Enter word(s) to convert to Pig Latin: ").upper()

print(f"In Pig Latin, {words} is: {piglatin(words)}")

CodePudding user response:

You are missing "i" in for loop;

words = input("Enter word(s) to convert to Pig Latin: ").upper()
wl = words.split() # words list

c = ("B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "X", "Z") # consonant list
v = ("A", "E", "I", "O", "U", "Y") # vowels list
a = "ay"
w = "way" # in your problem statement "yay" is written (have a look)... what should come way or yay for vowels

def piglatin(self):
    pt = []
    for i in range(len(wl)):
        if wl[i][0] in c:
            word = wl[i][1:]   wl[i][0]   a   #edited here
            pt.append(word)
        else:
            word = wl[i][1:]   wl[i][0]   w   #edited here
            pt.append(word)
        print(i)
    return " ".join(pt)

print(f"In Pig Latin, {words} is: {piglatin(words)}")


# output;
In Pig Latin, HI HEY HELLO EOP is: IHay EYHay ELLOHay OPEway

CodePudding user response:

i am getting the proper results for hi hey hello but the vowel translation is still moving the letter.

i suggest deleting the wl[0][0] after the else statement

words = input("Enter word(s) to convert to Pig Latin: ").upper()
wl = words.split() # words list

c = ("B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Z") # consonant list
v = ("A", "E", "I", "O", "U", "Y") # vowels list
a = "ay"
y = "yay"

def piglatin(self):
    pt = []
    for i in range(len(wl)):
        if wl[i][0] in c:
            word = wl[0][1:]   wl[0][0]   a
            pt.append(word)
        else:
            word = wl[0]   y
            pt.append(word)
        print(i)
    return " ".join(pt)

print(f"In Pig Latin, {words} is: {piglatin(words)}")
  • Related