PygLatin>PygLatin2>Ending Up


#1

pyg = 'ay'
original = raw_input('Enter a word:')
word=original.lower()
first=word[0]
new_word=word+first+pyg
second=new_word[1:len(new_word)]

if len(original) > 0 and original.isalpha():
word=original.lower()
first=word[0]
second=new_word[1:len(new_word)]
new_word=word+first+second+pyg
print new_word
else:
print 'empty'

In this code above I want to make the inputted name according to PigLatiin Game, but my concern is that
in "first=word[0]", WHAT EXACTLY HAPPENED?

it is giving us the word on 0 by doing the indexing, but I wanna to know is that indexed word will be erased when the program will be executed.?


#2

What you're doing on the line first = word[0] is, as you mentioned, indexing.
When you use indexing on a string, the rest of the string becomes irrelevant. You are only getting one letter in this case, so you assign that letter to the variable first and ignore the rest of the string.

If this helped, please be so kind as to select this answer as the solution. Thanks :slight_smile:


#3

Hey, what i want to say is that:

Here it is told:

Pig Latin is a language game, where you move the first letter of the word to the end and add "ay." So "Python" becomes "ythonpay" To write a Pig Latin translator in Python,

Here, it is mentioned above that in the output of the game you got "ythonpay"..OK.
So, in my code i got the output "pythonpay".

Why I don't get the output "ythonpay".

Please help me in the code that i had uploaded earlier in above conversation.


#4

Hey, so lets see what you are doing wrong here.
There won t be a code as second=new_word[1:len(new_word)] You add the new_word ,first and pyg as follows.

new_word = word[1:len(word)] + first + pyg

Just add that and remove:
second=new_word[1:len(new_word)]


#5

I h've done it..

Here is my code:

pyg = 'ay'
original = raw_input('Enter a word:')
word=original.lower()
first=word[0]
second=word[1:len(original)]
new_word=second+first+pyg
if len(original) > 0 and original.isalpha():
print new_word
print word[1:2]
print word[1:3]
print word[1:4]
print word[1:5]
print word[1:6]
print word[1:7]
print word[1:8]
else:
print 'empty'


#6

Nice, You created another variable. Good idea although i don t understand why you did this :confused:

print word[1:2]
print word[1:3]
print word[1:4]
print word[1:5]
print word[1:6]
print word[1:7]
print word[1:8]

#7

Its just like experimenting..What programming rules says "Our task has to be done, that doesn't matter how it is..that's in the brain of programmer.:slight_smile:


#8

i = raw_input(">")
print i[::-1]+"ay"


#9

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