Skip to content

Instantly share code, notes, and snippets.

@alex-born
Forked from JPierceM3/Lab 7b
Last active October 15, 2019 02:29
Show Gist options
  • Save alex-born/1b7462650de3551c8851ad05b5994807 to your computer and use it in GitHub Desktop.
Save alex-born/1b7462650de3551c8851ad05b5994807 to your computer and use it in GitHub Desktop.
print("This Program converts to Pig Latin, then prints both the original word and the Pig Latin version to the console. The program continues reading words until the user enters 'stop'.")
def pigLatin(ll):
flag = 0
if(ll[0] == "a" or ll[0] == "e" or ll[0] == "i" or ll[0] == "o" or ll[0]== "u" or ll[0] == "y"):
flag = 1
elif(ll[0] == "A" or ll[0] == "E" or ll[0] == "I" or ll[0] == "O" or ll[0]== "U" or ll[0] == "Y"):
flag = 1
if(flag == 1):
s = ll[1:] + ll[0]
s = s + "ay"
# All you need to do here is to write an else block with what happens if the flag is not set.
# The issue is that if the start of the word does not start with a vowel, s is not defined.
# Example s("hi") -> Error
# Example s("i") -> iay
# So add something like:
# else: s = ll + 'yay'
return(s)
s = ""
while(s!= "stop"):
s = input("Enter a word to convert to pig Latin enter stop to end: ")
if(s != "stop"):
p = pigLatin(s)
print(s +" is pig latin is: " + p)
@alex-born
Copy link
Author

As I was saying, a cleaner option might be to do something like this:

startsWithVowel = word[0].lower() in {'a', 'e', 'i', 'o', 'u', 'y'}
if(startsWithVowel): s = word[1:] + word[0] +  'ay'
else: s = word + 'yay'

@alex-born
Copy link
Author

Make sure you handle the case in which ll = the_word = ''

@JPierceM3
Copy link

word = " "
while(s!= "stop"):
s = input("Enter a word to convert to pig Latin enter stop to end: ")
if(s != "stop"):
startsWithVowel = word[0].lower() in {'a', 'e', 'i', 'o', 'u', 'y'}
if(startsWithVowel):
s = word[1:] + word[0] + 'ay'
else:
s = word + 'yay'
print(s, " is pig latin is: " )

@alex-born
Copy link
Author

word = ""
while(word != "stop"):
    word = input("Enter a word to convert to pig Latin enter stop to end: ")
    if(word != "stop"):
        startsWithVowel = word[0].lower() in {'a', 'e', 'i', 'o', 'u', 'y'}
        if(startsWithVowel): pigWord = word + 'yay'
        else: pigWord = word[1:] + word[0] + 'ay'
    print(word, " is pig latin is: ", pigWord)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment