10. Ending up


#1

Below is my code. I have no idea why it will not work. Very confused

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 original
else:
print 'empty'



Replace this line with your code.


#2

I am seeing a few issues with your current code. First off, I don't see why you are trying to print the original word after validating that it is longer than 0 and alpha.

Looking at my code I have that if statement wrapped around all of the sting modification. that way we only modify things that are >0 in length and alphanumeric. Further you need to do some string manipulation to trim off that first letter.

eg.
pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha(): #only manipulate words that are len>0 and alphanumeric
word=original.lower()
first=word[0]
new_word=word+first+pyg
new_word=new_word[1:len(new_word)] #trim off the first letter of the newly pieced together word
print new_word
else:
print 'empty' #print 'empty' if nothing was entered.


#3

pyg = 'ay'

original = raw_input('Enter a word:')

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

Allow me to explain as best I can.

new_word is a function and you can make it do whatever you want.

My name is Derek.
original = Derek

[1:len(original)] = "erek"
first = "d"
pyg = ay

I made sure to print new_word, not original, like they wanted.

Hope this helped.


#4

ok. I understand what you are saying, but now it keeps saying there is a syntax error on 'else:' I don't know why. I have retyped it several times and tried it with and without the colon.


#5

ok. when I read your code from top to bottom I understand it, but now it keeps saying there is a syntax error on 'else:' I don't know why. I have retyped it several times and tried it with and without the colon.


#6

type it like this(it worked for me) - btw im still new to programming so i cant really explain how this works

pyg = 'ay'

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

and don't forget to press Tab(or 4 time space) before "print" word


#7

I found that on line 6, (for your code) where it reads ;
new_word = original[1:len(original)] + first + pyg,
(original) needs to be (new_word)


#8

i figured it out. I didn't have the proper indentations. duh. thanks!


#9

I need help!!

I have read some of the replies but still do not know how to slice !! It keeps telling me that I thought that I did that already! Thanks!!

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


#10

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