Pyg latin translator doesnt translate properly


#1

I have added logic to the pyg latin translator to only add an ay if the letter starts with a vowel (as this is how it was spoken when we were kids) problem is it does it to everyword including those that don’t start in a vowel so all it does is add ‘ay’ at the end of the word, can anyone spot something i’ve missed:

pyg = 'ay'

original = raw_input('Enter a word: ')

if len(original) > 0 and original.isalpha():
  word = original.lower()
  first = word[0]
  if first == "a" or "e" or "i" or "o" or "u" :
      new_word = word+pyg
      print new_word
  else:
      new_word1 = word[1:len(word)]+first+pyg
      print new_word1
elif len(original) == 0:
  print 'empty'
elif not original.isalpha() :
  print "please only use alphabetic letters"
else: print "I don't understand"

#2

This exercise does not require the vowel rule so remove that part.


#3
else:
    new_word1 = word[1:len(word)]+first+pyg
    print new_word1

There’s your trouble.
Think about what you actually want your else statement to do
Currently you are adding pyg to every possibility of your if statement


#4

No it doesn’t but this is just for my own learning, any help is greatly appreciated


#5

I still can’t see the problem? I’m taking the slice of the word, then adding the firsr letter and then adding pyg? Which is what I want it to do for words that don’t start in a-e-i-o-u?


#6

You are over complicating your problem.
For words that start with a vowel you want to return the word + pyg(‘ay’)

What do you want to do if the word doesn’t start with a vowel?
Nothing right?
SO why have any code other than return word ?


#7

If it doesn’t start with a vowel, then i want it to return the pyg latin version of the word i.e. the word ‘very’ becomes ‘eryvay’, ery: word[1:len(word)] + first + pyg

This code was working until i added the code for vowels, after which it all seems to be translated as if starting with a vowel.


#8

Look what your else statement does…it adds pyg to the variable new word then you return that variable. Of course everything ends in ay, you’ve told the cpu “If it starts with a vowel add pyg, if not add pyg.”


#9

The problem isn’t that it ends in ‘ay’. The problem is if the word starts with a vowel, it should not move the first letter to the end of the original word. If the word Doesn’t start with a vowel, then it SHOULD move the first letter to the end of the original word, and add an ay to the end!

Currently all it does is add an ‘ay’ - without moving the first letter for words that DON’T start with a vowel!


#10

So it’s working as intended for vowels?

For non vowels, don’t bother slicing.
Something like
word += word[0]
word += pyg
Does what you described


#11

Thanks fight_dragons, that would be close but the word will still have the first letter which I dont want… i’ll go back to the drawing board with this one


#12

The exercise is inline code but in real world application we would be better to make it into a function so it’s reusable.

def pyglatin(word):
    if not len(word): return 'empty'
    if not word.isalpha(): return 'only alpha characters are allowed'
    pyg = 'ay'
    first = word[0]
    if first in 'aeiouAEIOU': return word + pyg
    return word[1:] + first + pyg

print (pyglatin('surprise'))    # urprisesay

Just for example.


#13

Thank @mtf I am following the course in order so functions isn’t a part that I’ve been introduced to yet


#14

I have figured out the problem, it seems the way that I am implementing my conditional statements is incorrect and thus seems to be ignored and the code will go straight to the next line as if it is true, however, replacing:

if first == "a" or "e" or "i" or "o" or "u" :

with

if first == 'a' or first == 'e' or first == 'i' or first == 'o' or first == 'u':

makes the code work as expected.


#15

Correct, you’re only comparing first to 'a' there


#16

Ah, hadn’t considered that.

original = raw_input("Enter a word to translate: ")
if not len(original): print 'empty'
elif not word.isalpha(): print 'only alpha characters are allowed'
else:
    pyg = 'ay'
    word = original
    first = word[0]
    if first in 'aeiouAEIOU': print (word + pyg)
    else: print (word[1:] + first + pyg)

#17

It’s strange that every word was treated as if this condition was true, am I right in thinking that because the rest of the condition didn’t make sense, it was just treated as true? Why wasn’t a syntax error given? Thanks for your help, glad I got it sorted :slight_smile:


#18

Thanks for your help, functions seem to make life a lot easier!


#19

Python is duck-typed, meaning it doesn’t necessarily care about what something is, and more about how it behaves.

When you treat a value like a boolean, then its truthiness gets checked. Non-empty strings are truthy.


#20

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.