Am I doing my slice method correctly in this python pyg latin exercise?


#1

Here is the actual exercise:
hello

The suggestion is that I use the following method

first we create a variable s and give it the string “Charlie”
Next we access the first letter of “Charlie” using s[0]. Remember letter positions start at 0.
Then we access a slice of “Charlie” using s[1:4]. This returns everything from the letter at position 1 up till position 4.
We are going to slice the string just like in the 3rd example above.

TLDR:

and this example works, according to the system, and still is incorrect (should print out igpay)

pyg = 'ay'

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

alternatively, the instructions say I need to use the following:

pyg = 'ay'

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

#2

It appears that you’re printing out the original word minus the first letter.

Meanwhile, your new_word is being set to the original word (including the first letter) plus the first letter, then finally adding pyg to the end.

For the desired results, you will want to print new_word where new_word is the word (minus the first letter), then add first, then pyg.

Hope this helps! Feel free to reply if otherwise. :smile:


#3

Would you suggest that I look up how to add a letter to the end of a word?

new_word = word - first + pyg

The above shows the word but I don’t know how to add it to the end of the current word.
The rest of the code stays the same.


#4

Your original splice with original[1:] was nearly correct. It was just misplaced and could be improved with your more accurate variable word.
We want new_word to equal (word - first letter) + (first letter) + (“ay”).
To have the correct (word - first letter), you just have to splice word to return all but the first letter.
After that, adding first and then adding pyg will be attached to the end of the spliced word because + in Python will concatenate (combine) strings together to make a single string.

Hope this clears it up! Otherwise, feel free to reply. :smile:


#5
pyg = 'ay'

original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
 word = original.lower()
 first = word[0]
 new_word = word + original[1:3] + pyg //what is happening here?
 print: original
else:
  print 'empty'

#6

Why i this one not currently working for me? I copied and pasted from the instructions


#7

What if I use 1:3 or 1:4?


#8

Thee instructions (on the website) mention using thee number but I do not understand.

First we create a variable s and give it the string “Charlie”
Next we access the first letter of “Charlie” using s[0]. Remember letter positions start at 0.
Then we access a slice of “Charlie” using s[1:4]. This returns everything from the letter at position 1 up till position 4.

s = "Charlie"

print s[0]
# will print "C"

print s[1:4]
# will print "har"

#9

I am leaving for bible study but will come back. I am interested in this question still!


#10

Alright, just went back to see exactly how the lesson wants it to be done and you’re very close with this.

The lesson wants you to change new_word to be the spliced version of itself. Like where you have print original[1:len(new_word)], if you change original to new_word you should be good to go. :+1:

Original was useful for checking that something was typed and only alphabetical characters were used. However, after going inside the if-statement, word becomes the new original.
Your current print original[1:len(new_word)] will take pig (original) and take the splice of the second element (i) to the end (g) to make the output: ig. Instead, if you splice the new_word (current pigpay) then the output will take the second element (i) to the end (y) to make the output: igpay.

Hope this clears things up! If not, feel free to reply. :smile:


#11

I don’t think the exercise said anything about printing, i think its trying to show the process step by step of how to work the formula.

only thing i see missing is

  new_word = new_word[1:len(new_word)]

How its placed is what the exercise is trying to teach.

#example
word = 1
word = word + 1 + 2

the word variable would return to 4


#12

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