I have a problem with the line of code provided by codecademy in exercise 10 of 11, "ending up." The instructions call for the use of new_word=word[1:len(new_word)] This line is slicing from index one up to the length of new_word. Shouldn't it be slicing from the variable "word" not "new_word?" The code works correctly either way but slicing from a variable "new_word" which has not been fully defined yet doesn't make sense. It would include the length of new_word as well as the length of first and pyg. I would think you would be slicing from the length of the variable "word" Am I missing something?
I don't know what your complete code is but here is mine:
pyg = 'ay' original = raw_input('Enter a word:') if len(original) > 0 and original.isalpha(): word = original.lower() first = word new_word = word + first + pyg new_word = new_word[1:len(new_word)] print new_word else: print 'empty'
new_word is defined with: new_word = word + first + pyg
The next line gives new_word a slice of itself as a new value
I hope this makes sense otherwise please post your code.
Thank you for responding. Here was my confusion. In exercise #9 "Move it on Back" The instructions were: Create a new variable called new_word and set it equal to the concatenation of word, first, and pyg. No problem!
Then in step #10 the instructions were: Set new_word equal to the slice from the 1st index all the way to the end of new_word. Use [1:len(new_word)] to do this.
It was the use of the word SET in the instructions that confused me.
In your code you first created the variable new_word= word +first+pyg and then you created another line where you modified new_word to new_word = new_word[1:len(new_word)]
I took the word SET in the instructions to mean modify the existing variable, not create a new line with the modified variable. This is why I asked how the new_word variable could be checked for length in the same line that was creating it.
I love code academy but the instructions are often vague and confusing. I am a middle school teacher and my classes are learning Python. I have to be able to explain every line of the code to young students. I have more trouble trying to understand what code academy is asking than I do understanding the Python code itself.
Thanks again for the clarification.
what is this part of code for
In pig latin you have to separate the first letter of the word from the remainder of the word. This line is used to separate the first letter from the rest of the word.
As an example if you entered the word truck,
The [1: tells the program to slice from index number 1. (Index 1 is actually the second letter of the word truck, the letter r. The t is located at index zero, index zero is actually the first index, it's confusing I know) This portion of the code line has separated the t from the rest of the word.
The len(new_word)] portion of the line tells the program to slice from the r up the the length of the variable new_word. Since the variable new_word is longer than the remaining letters in the word truck the result is that the letters ruck are sliced from the word truck.
Then the letter t from the variable first is added at the end followed by the letters ay from the variable pyg.
new_word=[1:len(new_word)] + first + pyg
The word truck will be translated to rucktay
I hope that makes sense.
If you want increase your knowledge of Python, google-search
== the Book ==
[your question] site:docs.python.org
string slice site:docs.python.org
https://docs.python.org/2/tutorial/introduction.html (and choose string from table-of-content )
I 'm not sure my answer made any sense so I rewrote it.
Line 1 – pyg=’ay’ In pig Latin every word must end with the letters “ay”. Line 1 creates a variable named pyg that contains the ay string.
Line 3 – original=raw_input(“Enter a word:”) Line 3 is used to ask the user to type in a word. The raw_input() command is used to prompt the user to enter information. The information that is entered by the user is stored in a variable named original. For our example let’s use the word truck.
Line 5 - if len(original) >0 and original.islpha() An if statement is created on line 5 and checks to make sure that two conditions have been met.
1: Has the user typed in a word? The user is required to type in a word but instead may just hit the enter key. The program checks to see that the user typed in a word by checking the length of the word typed. It must be greater than zero. If len(original)>0
2: The if statement also uses the and operator to check that the user only typed in alphabetical characters. Numbers and symbols are not accepted.
Remember the if statement must end with a colon.
Line 6 – print original Note that line 6 is part of the if statement introduced in line 5. Line 5 ended with a colon so line 6 must be indented 4 spaces. If both conditions of the if statement are met the program will print the contents of the variable named original. Since we entered the word truck which is all alphabetic the conditions of line 5 were met and the program will display the word truck.
Line 7- word=original.lower() Line 7 creates a new variable named word.
The purpose of line 7 is to convert the content of the variable original to lower case letters. This will ensure that the pig Latin word will not contain any capitals. Note that line 7 is part of the if statement and is indented like line 6.
Line 8 – first=word Line 8 creates a new variable named first. This line is used to identify the first letter in the word that was entered by the user. The  indicates that word consists of the letter located in index zero. The t is located in index zero. (remember, indexes start at zero, not 1). Now the variable first contains the letter t.
Line 9 – new_word= word + first +pyg Line 9 creates a variable names new_word. The purpose of line 9 is to re-assemble the word that the user entered in the proper order for pig latin. It will result in the original word, then the first letter of the original word and then the letters ay will be added to the word. The contents of new_word are trucktay.
Line 10 – new_word= new_word[1: len(new_word)] Line 10 is probably the most confusing. The purpose of line 10 is to slice the t off of the beginning of the word trucktay that was created in line 9. The variable new_word is being altered. The [1: portion of the code is telling the program to slice the variable starting with index 1. Index 1 is the letter r (remember indexes start at zero so index 1 is actually the second letter.)
The len(new_word)] portion of the code is telling the program to count the number of characters in the variable new_word . New world has 8 characters.
If you substitute an 8 into the len statement of the code it would read:
new_word= new_word[1:8] This would change the content of new_word by starting at index 1, the r and include all of the letters up to, but not including index 8. So new_word now contains the letters rucktay
Truck becomes rucktay.
Line 11 – print new_word Line 11 will print the contents of the variable new_word.
Line 12 – else: Line 11 is the else line that breaks the if statement. When the conditions of the if statement are NOT met, line 11 will be executed. It is placed under the if statement, and is not indented.
Line 13 – print “empty” If the else condition is met, line 12 prints the word empty. Note that it is indented 4 spaces because of the colon at the end of line11.
Did you do this?? cause i'm also a bit confused in this step
i have a problem with line 10
could you please help me..
Please present the code you are using upontill now....?!
Here is the code I used.
In my example I am using the word "truck" as the word the user enters. In pig latin the first letter "t" must be moved to the end of the word truck. Line 9 does this and also puts the letters "ay" at the end of the word so the word is now trucktay.
The purpose of line 10 is to remove the first t from trucktay so that you end up with rucktay. Line 10 does this by separating the t using a slice. The slice will start at, and include index 1. This is what the [1: in line 10 does. Index 1 is actually the second letter of the word because index numbers start at zero.
t is at index zero
r is at index 1
u is at index 2
c is at index 3
k is at index 4
t is at index 5
a is at index 6
y is at index 7
The slice then uses the length of the variable new_word that was created in line 9 to determine the ending point of the slice. The word trucktay is 8 characters in length. So now the ending number for the slice is an 8. If it makes it easier you can think of the new line 10 as new_word=new_word[1:8]. This will slice the word trucktay starting at index 1 which is the r all the way up to but not including index 8. So the variable named new_word will consist of :
index 1 = r
index 2 = u
index 3 = c
index 4 = k
index 5 = t
index 6 = a
index 7 = y
The question then becomes why not just use new_word=new_word[1:8] for line 10 instead of the code provided. The answer is because the users will be entering words of all different lengths of that's why len (new_word) is used.
Oh ok now i understood!! Thank you!
Thank you, you saved me dude
Sorry but we re supposed to write all these? when did we learn? im a beginner and i dont even know what is is.alpha .
I don't know if you figured this out yet, but your understanding of what is going on seems kinda limited.
To make a pig lattin word all that needs to be done is,
- Move the first letter to the end of the word
- Add 'ay' to the end of that.
- Length of the word needs to be greater than 1
def pig_latin(a_string): return ' '.join([(word[1:] + word + 'ay') if len(word) > 1 else word for word in a_string.lower().split()]) \ if any(' ' == item for item in a_string) \ else (a_string.lower()[1:] + a_string.lower() + 'ay')\ if len(a_string) > 2 else a_string
This code will convert any string into a pig latin translation of said word. Does not matter it's length or anything. I could add more checks to see if the there were numbers or special characters and do something with them but right now it is not required.
a = 'A string is here' b = 'Alpha' c = 'Python Rules' for item in a, b, c: pig_latin(item) # OUTPUT: 'a tringsay is erehay' # 'lphaaay' # 'ythonpay ulesray'
Also we need something to put back our translated works into English!
def pig_2english(a_string): return ' '.join([(word[-3] + word[:-3]) if len(word) > 1 else word for word in a_string.split()])\ if any(' ' == item for item in a_string)\ else a_string[-3] + a_string[:-3]\ if len(a_string) > 1 else a_string
As you can see if you read this code, not once do I use the
len() function to find the end of the string. Using slices default values which happen to be
[0:-1:1] we are able to assume what position it will start and end with and what it's stride will be.
Do not get bewildered by the above code samples, I am using list comprehension and ternary functions to achieve this. You are more than welcome to google either and find out how they work.
i cant understand nothing.I ll try to experiment myself and search around.Thanks, however
Thanks, I'm sure there are many ways to translate to pig latin. The only reason I used the line len(new_word ) was the instructions asked you to do so.
Set new_word equal to the slice from the 1st index all the way to the end of new_word. Use [1:len(new_word)] to do this.
I am not a programmer just a guy trying to follow vague instructions.
Unfortunately, code academy doesn't always do a good job of explaining the exercises. The .isalpha line will check to see that the user has entered only alphabetic characters, no numbers or symbols. The only way I found it was to google it for an explanation.
Use this code
pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
new_word= word + first + pyg
Your code does not remove the first letter from the finished product. Your variable new_word just adds the first letter and ay to the end of the word, It never removes the first letter from the word, so if the word you entered was truck the result would be trucktay. The result should be rucktay.