PygLatin Multiple Strings Help


#1


Hi everyone, I am looking to expand my code in order to take multiple strings such as "Good Morning"
Any help would be appreciated.


pyg = 'ay'

while True: 
    original = raw_input('Enter a word:').lower() #converts input to lower case up here instead of in the if statement

    if len(original) > 0 and original.isalpha():
        first = original[0]
        if first in'aeiou':
            new_word = original + pyg
            print new_word
            break
        else: 
            new_word = original + first + pyg
            new_word = new_word[1:] #we do not need to specify :len(new_word). It already knows to go to the last character
            print new_word
            break
    else:
       print "That is not a valid option. Please try again."


#2

let me get this right, you want for hello world that it prints:

elloay orlday

Do i understand that correctly?


#3

Yes that is exactly right!


#4

First problem to deal with is this:

and original.isalpha():

since spaces are not alpha. Someone in stackoverflow comes with this good solution:

if all(x.isalpha() or x.isspace() for x in string):

Hm.. might be a bit tricky, you know how this line works?


#5

I wasn't quite able to figure that out. But I am trying to use a for loop to accomplish this and I almost have it but I am having trouble getting it to iterate over multiple words

pyg = 'ay'

while True: 
    original = raw_input('Enter a word:').lower() #converts input to lower case up here instead of in the if statement
    no_space = original.replace(' ', '')
    original_array = original.split(' ')

if len(original) > 0 and original.isalpha() and len(original) == len(no_space):
    first = original[0]
    if first in'aeiou':
        new_word = original + pyg
        print new_word
        break
    else: 
        new_word = original + first + pyg
        new_word = new_word[1:] #we do not need to specify :len(new_word). It already knows to go to the last character
        print new_word
        break
elif len(original) > 0 and no_space.isalpha() and len(original) > len(no_space):
    for i in original_array:
        first = original_array[0][0]
        if first in 'aeiou':
            new_word = original_array[0] + pyg
            print new_word
            break
        else:
            new_word = original_array[0] + first + pyg
            new_word = new_word[1:]
            print new_word
            break
else:
   print "That is not a valid option. Please try again."

#6

Okay, maybe i might things slightly to complex, lets look at this code:

pyg = 'ay'

while True: 
    original = raw_input('Enter a word:').lower() #converts input to lower case up here instead of in the if statement
    no_space = original.replace(' ', '')
    if len(no_space) > 0 and no_space.isalpha():
        original_array = original.split(' ')
    else:
        print "That is not a valid option. Please try again."

You remove all the spaces from the string, this way, we can use isalpha() again, now we want to loop over the words in array, this can we can achieve with:

for i in original_array:
    print i

now i hold the words, so we should use i, not original_array[0], since that is always the first word, while i loops over the words. So you can get the first letter of each word with:

i[0]

so new word should be:
new_word = i[1:] + i[0] + pyg

i[1:]will give us the second till last letter.i[0]` the first, and pyg gives us pyg.


#7

Perfect! I also realized that my breaks weren't accurately placed (that's why it wasn't iterating).

This is the final code.

pyg = 'ay'

while True: 
    original = raw_input('Enter a word:').lower() #converts input to lower case up here instead of in the if statement
    no_space = original.replace(' ', '')
    original_array = original.split(' ')

    if len(original) > 0 and original.isalpha() and len(original) == len(no_space):
        first = original[0]
        if first in'aeiou':
            new_word = original + pyg
            print new_word
            break
        else: 
            new_word = original + first + pyg
            new_word = new_word[1:] #we do not need to specify :len(new_word). It already knows to go to the last character
            print new_word
            break
    elif len(original) > 0 and no_space.isalpha() and len(original) > len(no_space):
        for i in original_array:
            first = i[0]
            if first in 'aeiou':
                new_word = i + pyg
                print new_word
            else:
                new_word = i[1:] + first + pyg
                print new_word
        break
    else:
       print "That is not a valid option. Please try again."

#8

Nicely done, you could also make an array:

result = []

then instead of print each word:

print new_word

append it to the result list:

result.append(new_word)

and then ,just before you break join it together to create a sentence:

print " ".join(result)

#9

Is it supposed to print all of the new words on the same line?


#10

Yea, it is. Did you achieve this? I thought it is what you wanted?


#11

Ideally yes. The join method wasn't working for me (still can' figure out why) but by adding a comma to the end of print new_word (so it becomes print new_word,) appears to be working

`pyg = 'ay'

while True: 
    original = raw_input('Enter a word:').lower() #converts input to lower case up here instead of in the if statement
    no_space = original.replace(' ', '')
    original_array = original.split(' ')

    if len(original) > 0 and original.isalpha() and len(original) == len(no_space):
        first = original[0]
        if first in'aeiou':
            new_word = original + pyg
            print new_word
            break
        else: 
            new_word = original + first + pyg
            new_word = new_word[1:] #we do not need to specify :len(new_word). It already knows to go to the last character
            print new_word
            break
    elif len(original) > 0 and no_space.isalpha() and len(original) > len(no_space):
        for i in original_array:
            first = i[0]
            if first in 'aeiou':
                new_word = i + pyg
                print new_word,
            else:
                new_word = i[1:] + first + pyg
                print new_word, 
        break
    else:
       print "That is not a valid option. Please try again."
       `

#12

This will work also :slight_smile: You could try to optimize the code? I had some time left the past two days, so i wrote an advanced solution:

pyg = 'ay'
original = raw_input('Enter a word:').lower()
if len(original) > 0 and all(x.isalpha() or x.isspace() for x in original):
    result =  [x + pyg if x[0] in 'aeiou' else x[1:] + x[0] + pyg  for x in original.split(" ")]
    print " ".join(result)
else:
   print "That is not a valid option. Please try again."

Now, you don't have to go this far, but maybe it is a fun challenge to optimize your code?