Pyg-Latin ... don't get any of this topic at all


#1

I still don't get any of this topic at all!!! WHAT DO I DO???


Pyg = 'ay' first = word[0] 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'
#2

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.

  1. variables
  2. assignments
  3. strings
  4. concatenation
  5. if..else conditionals in control flow
  6. printing and console output

Some that may not be so clear..

  1. string indexes
  2. logical expressions in conditionals
  3. built in String class methods
  4. user input validation
  5. string slicing

Our finished program is going to have five variables in all, which we define in a specific order.

  1. pyg
  2. original
  3. word
  4. first
  5. new_word

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[0]

[0] is the index of the first character in the string, word.

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.

print new_word

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.


#3

Thank You.
I was not following that at all either.


#5

oh. nevermind i get it now


#6

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