Need help! Very hard for me


Oops, try again. It looks like new_word contains "hellohay" instead of "ellohay"!

I need the correct answer! What is wrong?

Replace this line with your code. 
pyg = 'ay'

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


here you take the slice:

new_word [0:len(new_word)]

you should store the result of this action new_word, and then print it on the next line


set the

new_word = word + first + 'pyg'[1:4]


You are on the right track! Try not to be too hard on yourself with this. It's common for people to be stuck when they are starting out with this.

As dumb as this sounds, the answer is actually in the instruction:

Set new_word equal to the slice from the 1st index all the way to the end of new_word. Use [1:len(new_word)] to do this.

You have new_word already = to word + first + pyg, so what you want to do, as per instruction, take the slice from the first new_word and set it equal to new_word again, but you want to add on what @stetim94 mentioned. So it's going to look like this:

new_word = new_word[........], and you will replace the dots with what the instructions said.

Hope that works.


I don't know.I tried all of them and they didn't work!


Your code

pyg = 'ay'

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

Lets test your code by providing it input and matching it with our desirable output

Lets run your code by putting the input.
Lets say I input : python

After this code:

pyg = 'ay'

original = raw_input('Enter a word:')

1.You converted the input in lower case and stored in variable word.
2. You stored first letter of word that is p in variable first
3. You have created a new variable new_word and added (concatenated) word , first and pyg.
Think for a moment what does it gives you ?
word -- > python
first --> p
pyg ---> ay

so you get >

4.You check that length of string is greater than 0 and if it is consists of alphabetic letters.
as our input is python ,it satisfies both of your conditions

5.Inside body of your conditional statement ,You're returning slice of string pythonpay by cutting it from 0 (including first element ) till the end?
so it returns you original string again which is pythonpay
but you wanted your output as ythonpay!!!!

You see the devil (bug) there?
I hope this hint will work for ya! :slight_smile:

Even you debug that portion, There is one more bug waiting for you!

It is basically Matching your code with Codeacademy's formats problem not a bug really.


Is there something wrong with the programme. When I enter this code
pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
print original.lower()
print 'empty'

It says
File "python", line 12
SyntaxError: invalid syntax
meaning the error is the : sign


Follow the instructions, but when you get to exercise after print original, remove that line and write all the next lessons' code in that branch of the if statement.

pyg = 'ay'
# get user input
# validate user input
# set word to original.lower
# set first to word[0]
# create new_word
# modify new_word to be a slice of itself
# print new_word


take a look at this code:

"hello world"[0:6]

see how is specify the string (this can also be a variable containing a string) before the square brackets? This way, python knows what its taking a slice off, in you case, you specified you wanted a slice, but not of what, so python throws an error, because it doesn't know


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