Skip to content Skip to sidebar Skip to footer

Pig_latin Captitalization

Question: Pig Latin is a code that:- Adds 'ay' to every word. If the word starts with a consonant, move the first letter to the end. Final word should have the first letter as cap

Solution 1:

The main problem seems to be the word.islower() == "false", which will never be true. Instead, check word.islower() == False, or rather not word.islower(), or word.istitle(). Also, iscapitalize() should be capitalize(), and that second for loop seems useless (whether it is inside the first loop or after it, which is not clear from the question's indentation).

This should work:

for word in sentence.split():
    first_letter = word[0]
    if first_letter in consonants:
        pig = word[1:] + first_letter + "ay"else:
        pig = word + "ay"if word.istitle():
        print(pig.capitalize())
    else:
        print(pig)

Or shorter, using ternary ... if ... else ... statements:

for word in sentence.split():
    pig = (word[1:] + word[0] if word[0] in consonants else word) + "ay"print(pig.capitalize() if word.istitle() else pig)

Solution 2:

Fixed solution that cleans up your code and makes it more efficient. See comments in code.

sentence = input ("Type in your sentence here ")

#Use "not a vowel" instead of "is a consonant". 
vowels = ['a','e','i','o','u'] 

#Iterating through words of sentencefor word in sentence.split():
    first_letter = word[0]

    #Now you don't have to type out all the consonantsif first_letter not in vowels:
        pig = word [1:] + first_letter + "ay"else:
        pig = word + "ay"#No need for second loop, you're already iterating through each word in the sentence. #end=" " prints out a sentence instead of individual lines. ifnot pig.islower():
        pig = pig.title()
    print(pig, end=" ")

Resources:

Title case

islower()

Solution 3:

A variant on the many solutions offered that tries to both be (actually) efficient and produce correct, and correctly formatted, output:

sentence = input("Type in your sentence here: ")

vowels = set('aeiouAEIOU')

for word in sentence.split():
    first_letter = word[0]

    if first_letter in vowels:
        latin = word + "ay"else:
        latin = word[1:] + first_letter + "ay"print(latin if latin.islower() else latin.capitalize(), end=' ')  # fix case: Skip -> kipSay -> Kipsayprint()

USAGE

> python3 test.py
Type in your sentence here: Horton hears a Who
Ortonhay earshay aay Howay 
>

Solution 4:

Here are few problems I solved:

  • There was no need of Second loop as mentioned in other answers.
  • Also you need to add capital consonants as well
  • islower() == True as True is a boolean and islower() returns boolean. Also if will work if islower() returns True so I just wrote islower()
  • Also try to understand that islower() does not see the first letter but just returns True if any of the letter is capital.

sentence = input ("Type in your sentence here ")

consonants2 = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']

consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']

# comment the below 3 lines if you don't need to change format for words which starts with capital consonant letter. I 
# I added it because of your example Testay and Esstay which somehow suggest me that you want that.
for i in consonants2:
    consonants.append(i.capitalize())


for word in sentence.split():
    first_letter = word[0]

    if first_letter in consonants :
        pig = word [1:] + first_letter + "ay"
    else :
        pig = word + "ay"
    if pig[0].islower():
        print(pig.capitalize())
    else:
        print(pig)

Post a Comment for "Pig_latin Captitalization"