Ending up


#1



What is wrong with my code> can any1 help me please?


pyg = 'ay'

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


#2

here:

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

why add first and pyg again?

also, you need to specify the string/variable containing a string before the square brackets, how else does python know what to slice?


#3

I triend this but doesnt work

pyg = 'ay'

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


#4

^, did you see this?


#5

Yes. I changed to :
pyg = 'ay'

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

but now i receive this : Oops, try again. The value for the original variable looks off, make sure that you entered a word containing only letters.


#6

here:

"new_word"[1:len(new_word)]

why is new_word a string?

also, after you take the slice, you need to store the result in the same varialbe (updating the variable)


#7

"also, you need to specify the string/variable containing a string before the square brackets"

how to specify it?


#8

also tried this

pyg = 'ay'

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


#9

like you did here:

new_word[1:]

now you have the slice, you need to store the slice in a variable, this variable should be named new_word


#10

pyg = 'ay'

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

doesnt work


#11

can you give me an example how to store in a variable?


#12

neither this

pyg = 'ay'

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


#13

we can take a slice and store it in a variable:

x = "hello world"[0:6]

#14

Doesn't work :confused:

pyg = 'ay'

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


#15

but in your case, word and new_word should be variables not string

I am sorry, but at this point in the course i assume you know the difference between string and variables


#18

I tried to put wors as a variable :

pyg = 'ay'

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

but i get this then :

Hi
Traceback (most recent call last):
File "python", line 5, in
IndexError: string index out of range


#19

what do you enter when prompted by raw_input?

If you want to do it right, you should place this:

word = original.lower()
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:]

in the if clause, where currently is print original. You only want to manipulate the string if valid


#20

Here is what I did:

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

I kept writing something like new_word = [1:len(new_word)] until I went through the lesson one more time and realized that their examples have a different syntax: var[1:5]. In other words, you need to make sure that it states what string is being indexed.


#21

Thanks for your post, it cleared things up for me.


#22

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