I still don't get any of this topic at all!!! WHAT DO I DO???
Pyg = 'ay' first = word original = raw_input('Enter a word:') if len(original) > 0 and original.isalpha(): if word.lower(original) > 0 and original.isalpha(): print original else: print 'empty'
This module is for extra study, and may introduce a couple of concepts we are not familiar with, yet. But, it's also to practice a few that we have covered so far.
- if..else conditionals in control flow
- printing and console output
Some that may not be so clear..
- string indexes
- logical expressions in conditionals
- built in String class methods
- user input validation
- string slicing
Our finished program is going to have five variables in all, which we define in a specific order.
pyg is a string constant:
pyg = 'ay'
original is where we cache user entered input.
original = raw_input("Enter a word: ")
Now comes the validation of user input.
len(orginal) > 0 # the input is not an empty string ( if this is true... ) original.isalpha() # the input contains no non-letter characters ( ditto )
Putting this together, we get,
if len(original) > 0 and original.isalpha():
then we are able to continue with the program
print original else: print "empty"
This is the setup for the remaining code. We can remove the line,
print original and begin inserting new code at that point. Leave the
else: where it is, at the bottom.
To preserve the original input, we copy a modified version to the variable,
word = original.lower()
word will now be a lowercase copy of original. This is what we'll be working with going forward.
We need the first letter of the word so we can place it near the end where Pyg-Latin would put it. Now comes the indexing concept, and the variable to which it is assigned,
first = word
 is the index of the first character in the string,
Next we will string together all the components above and assign that to the vaiable,
new_word = word + first + pyg
But now our word has the first letter repeated later. That means we need to remove the first letter, which is where slicing comes in.
new_word = new_word[1:len(new_word)] # edited
This copies the 2nd through last letter in
new_word back into
new_word. We have repurposed the variable and given it a new string. The same can be done with the much shorter form of syntax, but the instructions ask for the above.
new_word = new_word[1:]
When no end index is included, Python defaults to the last index so this string is identical to the one above.
All that is left to do now is print it.
Piece this all together and you will have working code. Be sure to follow the instructions and carry out each step as it is introduced. Use this page as a guide, not a template.
I was not following that at all either.
oh. nevermind i get it now
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.