10 Ending Up - Alternative to [1:len(new_word)]


#1

I googled how to select the last in an index and found that "word[1:]" can be used to mean first to last index of word. I've tried it and it works and is much simpler.


#2

it is written in the hints section


#3

pyg = 'ay'

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

I am on ending up and I am getting an error

Oops, try again. The value for the original variable looks off, make sure that you entered a word containing only letters.


#4

I need help to.:cold_sweat: Plz help me thx


#5

Hi, your code is all messed up. You need to create new variables (word, first) inside the if condition. Also, to access the words it should be

first = word[0], it is not "word"[0].

You don't need to create a new_word. I am giving sample of the code, please don't copy and paste, understand and try yourself, otherwise you won't learn.

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

Again understand and make something yourself.

Thanks


#6

Thanks alot for your help but it is all messed up & I'm not able to get it.
But I really appreciate for your time and guidance


#7

Okay. Tell me what is the problem you are facing?


#8

hello, i followed your instructions but im still getting an error message

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
new_word=[1:len(new_word)]
print new_word
else:
print 'empty'

Oops, try again.
** Your code did not run to end - see the console window for the error message.**

console message:
File "python", line 9
new_word=[1:len(new_word)]
^
SyntaxError: invalid syntax


#9

@methodsolver22375 Instead of doing it in two separate lines like this:

It is suppose to be like this:

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

Otherwise you are confusing the system with the different values of the variable "New_word"


#10

oh great..it worked

thanks.


#11

Happy to help :slight_smile:


#12

This is probably a dumb thing to be confused about but I was wondering why you put 'new_word' before [1:len(new_word)]. My code was simply: new_word = [1:len(new_word)]. After seeing your post I tried to make sense of the addition of an extra 'new_word', but ended up with nothing. Also, I am assuming that you replaced 'print original' with 'print new_word' because otherwise Python would have gotten confused with having two different variables like 'original' and 'new_word'. Is my assumption correct or is there another reason for the change?

Thank you in advance!


#13

No one is dumb to ask something. Well there are different ways to do the same thing, it's upon you to choose. Also, the code should be new_word = [1:len(new_word)] but some people get error while using this, It might be a python version error. (I have'nt looked at this so not sure if it's really a version error, might look at it in future.) Both ways are correct. Lastly,
print original
might not always work. We have given new_word a new value which is
new_word=word+first+pyg
, it is better to use new_word, it will print exactly what we require and yes, your assumption is somewhat correct.
Thanks for asking.


#14

I actually did get that error for 'new_word = [1:len(new_word)]' so your explanation was helpful. The 'print original' and 'print new_word' thing makes a little more sense to me now too. Thanks for the help!


#15

I need a help i am getting this error if I doesn't put any value in console.
Oops, try again. The value for the original variable looks off, make sure that you entered a word containing only letters.

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
new_word = word[1:len(word)] + first + pyg
print new_word

else:
print 'empty'


#16

This line is tripping you up:

You put two different values for the variable "New_word", which is why you are getting the error. Delete that line and you should be fine, assuming your indents are correct. :slight_smile:


#17

Thankyou :slight_smile:


#18

You're welcome :slight_smile:


#19

i can't get past this part

pyg = 'ay'

original = raw_input('Enter a word:')

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

wut am i doing wrong?


#20

Take a good look at your code. You use the commands needed to make the translator work only if the original input does NOT met the criteria of a blank input and has alphabetical letters. Instead you were suppose to place those commands under the if statement so that they are used once the criteria is justified. Rewrite that part of your code. It should look like this:

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 + first + pyg
new_word= word[1:len(word)] + first + pyg
else:
print 'empty'