Thomas
Thomas

Reputation: 73

Reference next item in list: python

I'm making a variation of Codecademy's pyglatin.py to make a translator that accepts and translates multiple words. However, I'm having trouble translating more than one word. I've been able to transfer the raw input into a list and translate the first, but I do not know how to reference the next item in the list. Any help would be greatly appreciated.

def piglatin1():

    pig = 'ay'

    original = raw_input('Enter a phrase:').split(' ')
    L = list(original)
    print L
    i = iter(L)
    item = i.next()


    for item in L:

        if len(item) > 0 and item.isalpha():
            word = item.lower()
            first = word
            if first == "a" or first == "e" or first == "i" or first == "o" or first =="u":
                new_word = word + pig
                print new_word
            else:
                new_word = word[1:] + word[0:1] + pig
            # first word translated    
                L = []
                M = L[:]


                L.append(new_word)

                print L # secondary list created.

                again = raw_input('Translate again? Y/N')
                print again

                if len(again) > 0 and again.isalpha():
                    second_word = again.lower()
                    if second_word == "y":
                        return piglatin()
                    else:
                        print "Okay Dokey!"

        else:
            print 'Letters only please!'
            return piglatin1()

Upvotes: 7

Views: 41025

Answers (4)

acknapp
acknapp

Reputation: 145

I was working on this problem recently as well and came up with the following solution (rather than use range, use enumerate to get the index).

for index, item in enumerate(L):
    next = index + 1
    if next < len(L):
        print index, item, next

This example shows how to access the current index, the current item, and then the next item in the list (if it exists in the bounds of the list).

Upvotes: 7

Nadia
Nadia

Reputation: 81

Step by step:

  1. If you set variable original in this way:

    original = raw_input('Enter a phrase:').split()
    

    it will be already a list, so need to additional assignment.

  2. What is the purpose of these lines?

    i = iter(L)
    item = i.next()
    
  3. In a loop, you assign variable to the word, when it is actually only the first letter of the word, so it’ll be better like this: first = word[0]

  4. Then if you want to check if first is a vowel, you can just do:

    if first in 'aeuoiy'
    
  5. Answer to your actual question: do not assign L to an empty list!

  6. If you want to repeat the action of a function, you can just call it again, no need to rewrite the code.

Upvotes: 0

RMcG
RMcG

Reputation: 1095

There are some slight issues with the code but I think there is one main reason why it will not repeat.

In order to process the entire string the

again = raw_input('Translate again? Y/N')

and it's succeeding lines should be brought outside the for statement. Also you appear to be setting L to an empty string inside the loop:

L = []

The following is a modified version of your code which will loop through the entire sentence and then ask for another one.

def piglatin():
    pig = 'ay'
    while True:
        L = raw_input('Enter a phrase:').split(' ')
        M = []
        for item in L:
            if len(item) > 0 and item.isalpha():
                word = item.lower()
                first = word
                if first == "a" or first == "e" or first == "i" or first == "o" or first =="u":
                    new_word = word + pig
                    print new_word
                else:
                    new_word = word[1:] + word[0:1] + pig
                    M.append(new_word)
            else:
                print 'Letters only please!'

        print M # secondary list created.
        again = raw_input('Translate again? Y/N')
        print again
        if len(again) > 0 and again.isalpha():
           second_word = again.lower()
        if second_word == "n":
           print "Okay Dokey!"
           break

Changes made:

  • You don't need to cast the return of the split to a list. The split return type is a list.
  • It isn't necessary to make an iterator, the for loop will do this for you.
  • I removed the function as the return type. I'm assuming you were attempting some form of recursion but it isn't strictly necessary.

Hope this helps.

Upvotes: 0

pravnar
pravnar

Reputation: 833

Here are a few things to note that might help.

  1. The lines i = iter(L) and item = i.next() are unnecessary. They have no effect in this method because you are redefining item immediately afterwards in the line for item in L. Go ahead and comment out those two lines to see if it makes any changes in your output.
  2. The looping construct for item in L will go once over every item in the list. Whatever code you write within this loop will be executed once for each item in the list. The variable item is your handle to the list element of an iteration.
  3. If, during any iteration, you really do want to access the "next" element in the list as well, then consider using a looping construct such as for i in range(0,len(L)). Then L[i] will be the current item and L[i+1] will you give the subsequent item.

Upvotes: 4

Related Questions