Python - Pyglatin exercise


#1

I am struggling to understand the Pyglatin code. It uses some functions that im troubling to find the definition to and it makes the syntax very confusing as a new learner. Can someone make some # comments on a solution so I can follow the logic?


#2

Which functions are confusing you?

if len(original) > 0 ?

if original.isalpha() ?

#3

Those for sure along with these lines:

if “aeiou”.find(first) != -1:

and…

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


#4
first = word[0]

This is the first letter of the inputed word.

For this exercise we don’t need to follow the first letter a vowel rule. Seems it mentions that in the instructions, but if not, then just don’t do it or it will not be accepted.

The find method will search a string for the argument and return its index in the string, or -1 if not found.

>>> f = 'a'
>>> 'aeiou'.find(f)
0
>>> g = 'u'
>>> 'aeiou'.find(g)
4
>>> 

However, as stated, this will not pass in the lesson, so put this on a back burner.

Not sure which lesson this covered in, but it should have been before this exercise. It’s what is called a slice.

word[1:len(word)]

str[first : last]

If word is ‘surprise’, then the above slice will be,

urprise

Note that it starts at index 1, not 0, and the last is not included in the slice.

surprise[1:4]  =>  urp
                   123  <=  index

#5

Thank you so much. Ill study this. It makes more sense. I did the python lessons as they were ordered and havent skipped anyting. I dont remember “slice” at all.

Thanks!


#6

Some examples…

First three letters

>>> 'surprise'[:3]
'sur'

Last three letters

>>> 'surprise'[-3:]
'ise'

Letter(s) between first three and last three

>>> 'surprise'[3:-3]
'pr'

String in reverse

>>> 'surprise'[::-1]
'esirprus'

#7

Lesson 10/11 of PygLatin (“Ending Up”) introduces the concept of a slice, but only by giving an example and saying “this is called a slice”. For the purposes of the Ending Up exercise, the instructions give you the exact slice that you need to make the code work but without much explanation of why it works.

@geren14 : list slicing as a concept is introduced in more detail in the Python Lists and Dictionaries lesson, starting with exercise 5/14 “List Slicing”, so don’t worry too much if they aren’t making complete sense just yet. :slight_smile:


#8

I am making an advanced version of it, but it is super hard. This is it so far(I use python 3):

print ('Which type of Pig Latin do you use? (How do you say the word about? There is no wrong answer.)')
print ('')
print ('')
print ('')
print ('A: about-ay')
print ('B: about-way')
print ('C: about-yay')
print ('D: yabout-yay')
print ('')
print ('')
print ('For those who have not used pig latin, A is the easiest to decifer.')
print ('B and C are the same, in the middle.')
print ('D is the hardest.')
pyg_latin_type = input('Type A, B, C, or D.')





def pyg_latin_w():
  pyg = 'ay'

  v_pyg = 'way'

  original = input('Enter a word(One word at a time):')

  if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]
    if len(original) == 7 and word[2] or word[3] or word[4] or word[5] or word[6] == 'y':
      new_word = word + '-' + first + word[1] + pyg
      new_word = new_word[2:len(new_word)]
      print (new_word)
      pyg_latin_w()
    elif len(original) == 6 and word[2] or word[3] or word[4] or word[5]== 'y':
      new_word = word + '-' + first + word[1] + pyg
      new_word = new_word[2:len(new_word)]
      print (new_word)
      pyg_latin_w()
    elif len(original) == 5 and word[2] or word[3] or word[4] == 'y':
      new_word = word + '-' + first + word[1] + pyg
      new_word = new_word[2:len(new_word)]
      print (new_word)
      pyg_latin_w()
    elif len(original) == 4 and word[2] or word[3] == 'y':
      new_word = word + '-' + first + word[1] + pyg
      new_word = new_word[2:len(new_word)]
      print (new_word)
      pyg_latin_w()
    elif len(original) == 3 and word[2] == 'y':
      new_word = word + '-' + first + word[1] + pyg
      new_word = new_word[2:len(new_word)]
      print (new_word)
      pyg_latin_w()
    elif word[1] == 'y':
      new_word3 = word + '-' + first + pyg
      new_word3 = new_word3[1:len(new_word3)]
      print (new_word3)
      pyg_latin_w()
    elif first in 'aeiou':
      new_word1 = word + '-' + v_pyg
      print (new_word1)
      pyg_latin_w()
    elif first and word[1] not :
      new_word = word + '-' + first + word[1] + pyg
      new_word = new_word[2:len(new_word)]
      print (new_word)
      pyg_latin_w()
    else:
      new_word2 = word + '-' + first + pyg
      new_word2 = new_word2[1:len(new_word2)]
      print (new_word2)
      pyg_latin_w()
  else:
      print ('That is empty or has numbers.')
      pyg_latin_w()

pyg_latin_w()

#9

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