10 Replies - 3054 Views - Last Post: 17 May 2014 - 05:24 PM Rate Topic: -----

#1 reptile guy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-May 14

coding piglatin in python help

Posted 17 May 2014 - 09:49 AM

I am new to coding, and am working on my first project in python, Piglatin
I have sucessfully been able to translate english words into Piglatin, but that will only work for a single word. Now I am trying to translate entire sentences. I am using .split() to seperate the sentence into seperate strings, but when I test this code i get this:

Welcome to Piglatin
Please enter a sentence: hello there
Traceback (most recent call last):
File "<stdin>", line 23, in <module>
AttributeError: 'list' object has no attribute 'isalpha'

here is my code:

ending = "ay"

vowel = ['a','e', 'i', 'o', 'u', 'A' , 'E', 'I', 'O', 'U']

print 'Welcome to Piglatin'

sentence = raw_input('Please enter a sentence:')

original_word = sentence.split(' ')

for word in original_word:
if len(original_word) > 2 and original_word.isalpha() and original_word[0] in vowel == true:
print original_word
word = original_word.lower()
new_word = word + "way"

elif len(original_word) > 2 and original_word.isalpha() == true:
print original_word
word = original_word.lower()
beginning = word[0]
new_word = word[1: len(original_word)] + beginning + ending

elif len(original_word) > 0 and original_word.isalpha() == true:
print original_word
word = original_word.lower()
new_word = word

print new_word

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: coding piglatin in python help

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2955
  • View blog
  • Posts: 11,472
  • Joined: 03-December 12

Re: coding piglatin in python help

Posted 17 May 2014 - 09:57 AM

The code takes your string (the sentence) and puts each word in the sentence into a list. So you need to iterate through the list of words (using a loop) to validate .
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5485
  • View blog
  • Posts: 14,448
  • Joined: 18-April 07

Re: coding piglatin in python help

Posted 17 May 2014 - 09:58 AM

Well split() is going to return a list, not a string. You need to be calling things like isalpha() on "word" not original_word. Here your labeling is not exactly accurate, you should probably call it word_list instead of original_word because that is a list of strings (being the words).

:)
Was This Post Helpful? 1
  • +
  • -

#4 reptile guy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-May 14

Re: coding piglatin in python help

Posted 17 May 2014 - 10:20 AM

okay so i think i fixed up what you said i had toending = "ay"

ending = "ay"

vowel = ['a','e', 'i', 'o', 'u', 'A' , 'E', 'I', 'O', 'U']

print 'Welcome to Piglatin'

sentence = raw_input('Please enter a sentence:')

word_list= sentence.split(' ')

for word in word_list:
if len(word_list) > 2 and word.isalpha() and word_list[0] in vowel:
word = word.lower()
new_word = word + "way"

elif len(word_list) > 2 and word.isalpha():
word = word.lower()
beginning = word[0]
new_word = word[1: len(word_list)] + beginning + ending

elif len(word_list) > 0 and word.isalpha():
word = word.lower()
new_word = word

print new_word

now when i enter in my sentence here is what i get:

Welcome to Piglatin
Please enter a sentence: hello there
hello
there

why were the words not converted this time?
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5485
  • View blog
  • Posts: 14,448
  • Joined: 18-April 07

Re: coding piglatin in python help

Posted 17 May 2014 - 10:28 AM

When you type "hello there" your len(word_list) is going to be 2. Is that greater than 2? No, so it skips over both the first if, the second if and lands on elif len(word_list) > 0 and word.isalpha(): which simply lowercases the word, sets new_word = word and you print out new_word... which has no modification to it.

:)
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2955
  • View blog
  • Posts: 11,472
  • Joined: 03-December 12

Re: coding piglatin in python help

Posted 17 May 2014 - 10:29 AM

use the [CODE] tags. with Python being an indention specific language it is hard to tell if you are in the loop or not.
Was This Post Helpful? 0
  • +
  • -

#7 woooee   User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 171
  • Joined: 21-November 12

Re: coding piglatin in python help

Posted 17 May 2014 - 11:55 AM

Add some print statements and test data to see what is happening (code that has not passed any simple tests does not deserve any responses). For example, the two elif statements:
elif len(word_list) > 2 and word.isalpha():

## and

elif len(word_list) > 0 and word.isalpha(): 
the second will always overlay the first. Also what happens if the word is not alpha? You might want to do something like
if word.isalpha():
    if:
        ...
    
    elif:
        ...
else:
    print ("""Do not enter any punctuation, or
other non-alpha characters""") 

This post has been edited by woooee: 17 May 2014 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#8 reptile guy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-May 14

Re: coding piglatin in python help

Posted 17 May 2014 - 12:16 PM

okay, now each word is translated correctly:

ending = "ay"

vowel = ['a','e', 'i', 'o', 'u', 'A' , 'E', 'I', 'O', 'U']

print 'Welcome to Piglatin'

sentence = raw_input('Please enter a sentence:')

word_list= sentence.split()

for word in word_list:
    if len(word) > 2 and word.isalpha() and word[0] in vowel:
        word = word.lower()
        new_word = word + "way"

    elif len(word) > 2 and word.isalpha() and word:
        word = word.lower()
        beginning = word[0]
        new_word = word[1: len(word)] + beginning + ending
    
    elif len(word) > 0 and word.isalpha():
        word = word.lower()
        new_word = word
    
    print new_word
    
else:
    'Please try again and make sure to not include punctuation or non-alpha characters'


and this shows up:
Welcome to Piglatin
Please enter a sentence: hello there
ellohay
heretay
 


so obviously progress has been made, but i still have one question, how do i use .join() to put these words back in one sentence on one line as opposed to a separate line for each word
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2955
  • View blog
  • Posts: 11,472
  • Joined: 03-December 12

Re: coding piglatin in python help

Posted 17 May 2014 - 01:15 PM

Sample code to look at. It is in Python 3, but the premise is the same.


Spoiler



I threw it together qiuckly so it is missing alot of functionality.

This post has been edited by astonecipher: 17 May 2014 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#10 reptile guy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-May 14

Re: coding piglatin in python help

Posted 17 May 2014 - 05:14 PM

okay, everything works perfectly now!!
here is the code used:
ending = 'ay'
vowel = ['a','e', 'i', 'o', 'u', 'A' , 'E', 'I', 'O', 'U']

print 'Welcome to Piglatin'

sentence = raw_input('Please enter a sentence:')

new_sentence = [ ]

word_list= sentence.split()

for word in word_list:
    if len(word) > 2 and word.isalpha() and word[0] in vowel:
        word = word.lower()
        new_word = word + "way"

    elif len(word) > 2 and word.isalpha() and word:
        word = word.lower()
        beginning = word[0]
        new_word = word[1: len(word)] + beginning + ending
    
    elif len(word) > 0 and word.isalpha():
        word = word.lower()
        new_word = word

    else:
        print 'empty'
     
    new_sentence.append(new_word)  
    str = " "
    new_sentence_2 = str.join(new_sentence)
    print new_sentence_2


i was wondering how this part works though:

new_sentence.append(new_word)  
    str = " "
    new_sentence_2 = str.join(new_sentence)
    print new_sentence_2


what exactly does append mean? what does it do?
Was This Post Helpful? 0
  • +
  • -

#11 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2955
  • View blog
  • Posts: 11,472
  • Joined: 03-December 12

Re: coding piglatin in python help

Posted 17 May 2014 - 05:24 PM

new_sentence is defined as a list. The append method adds each word to the list. That way when using the join method it has a sequence to go through. It then combines each item in the list with a separator of a space, making a new sentence.

This post has been edited by astonecipher: 17 May 2014 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1