Welcome to The Forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads

I Don't Understand (Questions)


Shaman
 Share

Recommended Posts

Alright so currently the website has me slowly creating a Pig Latin translator. Currently the script/code is as follows.

 

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

 

It is stating that I did the exercise correctly but I don't understand how it could be when it is returning the following

 

Enter a word: Hello
Hello
ello
None

 

From what I am seeing the screen should be returning "Ellohay" not just "Ello". I'm rather confused with this as to me it's not correct but the system is telling me it is and to move on. Can anyone shed some light on why it is doing this?

 

Note: I'm probably going to be making this a thread for when I have the odd question lol.

Link to comment
Share on other sites

s = "Hello"
print s[1:5]

i don't get why this is here. I'm assuming it's because you were testing (or were supposed to be testing out) how String arrays work? How [1:5] would take the word from point 1 (E) to point 5 (O).

 

See you are making a "new word", by taking all but the first character, then taking the first character and putting it in the end, then adding pyg (which is "ay"). The code you have does all that, but it never prints.

 

s = "Hello"
print s[1:5]
new_word = word[1:len(word)] + first + pyg


I think it should be

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


IMO you should go through and comment every line you can and say what it does and what it's purpose in the script as a whole is.

Edited by Icon315
edited to somewhat put together the wreckage i call a train of thought
Link to comment
Share on other sites

s = "Hello"
print s[1:5]

i don't get why this is here. I'm assuming it's because you were testing (or were supposed to be testing out) how String arrays work? How [1:5] would take the word from point 1 (E) to point 5 (O).

 

See you are making a "new word", by taking all but the first character, then taking the first character and putting it in the end, then adding pyg (which is "ay"). The code you have does all that, but it never prints.

 

s = "Hello"
print s[1:5]
new_word = word[1:len(word)] + first + pyg


I think it should be

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


IMO you should go through and comment every line you can and say what it does and what it's purpose in the script as a whole is.

 

That was how I originally had it, I changed it back as you can see here.

 

665d4592-1cfc-4c06-81cf-62a8f710f930_zpsifhalg6x.png

 

In the previous exercise it had wanted me to create the s[1:5] variable so it would actually print "ellohay".

 

This is supposed to be a pig latin translator but as you can see it is just returning whatever word I type in and claming it is correct. This is what is so confusing to me.

 

So it is actually doing what it is claiming just it's not printing it on screen?

Edited by Alten
Link to comment
Share on other sites

In the code you are showing you are not returning "new_word", you're creating it then not doing anything with it.

 

pyg = 'ay'
original = raw_input('Enter a word:\n')
if len(original) > 0 and original.isalpha():
//Removed the line that prints the original since it's not neccessary
   word = original.lower()
   first = word[0]
   new_word = word[1:len(word)] + first + pyg
   print new_word // This is the line that gives the output
else:
   print 'empty'

Link to comment
Share on other sites

Lol I should have realized I needed to add the print function in. I was thinking only about doing exactly what the exercise wanted which it hadn't stated that. Need to pay more attention. Also makes sense as to why it is saying pass when it wasn't printing it out on screen.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share