Where am I going wrong?


#1



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


#2

What's the problem? What behaviour should be different?
What is someone reading your question to look for?


#3

I'm getting this error:
File "python", line 6
new_word = [1:2 len(new_word)]
^
SyntaxError: invalid syntax


#4

Have a look at the examples in the instructions for what the syntax for slicing is


#5

I've tried : new_word[1:3] and didn't work


#6

What happened differently from what you wanted? (don't use the word "work" to describe something, that only works when the other person knows what you mean, and I don't, I'd argue that it does in fact work but perhaps I would use that piece of code in a different context from you - in which case I can't reproduce what you tried because I don't know what you tried)

describe and motivate everything!


#7

lol I did wrote down what I did that didn't work but somehow it didn't appear sorry

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


#8

Python will tell you that you're missing some indentation.
You'll probably argue that your code is different from that
Well, I'd argue that you should show that code instead

Also, still haven't mentioned what's different

And if it seems silly to explain what's going on, that's how you go about solving things! By identifying what's going on, and then thinking about what you can do with that information. So since that is the part you're not doing, that's the part I'll be responding to. Odds are that if you take the time to observe what's going on then you'll figure out the rest.

So, if you for example make the observation that there's no longer a syntax error (I don't know whether that's the case, I do not know what your code looks like), then you can conclude that you've got the syntax right.

The "working" part would then be about considering how slicing behaves and how you can leverage that - and then looking at your code and considering whether that is what you are doing


#10

Hi,
I had the same problem, but the answer was actually in the explanation and the given code in the exercise part wasn't complete:
in stead of [1:2len(new_word] you should use new_word[1:2len(new_word]. Which is actually quite logical because you should tell the program of which variable you are slicing.
Hope this helps!

(oeps deleted it by accident)


#11

I got it!

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


#12

first="word"[0] couldn't possibly be what you actually mean. Now you will always select the letter "w". What you want to do is take the variable word, so you want to drop the quotes. first = word[0].


#13

And also this line

new_word = word[1:4] + first + pyg
Works only when you are using words that use only 4 letters. Because with word[1:4] you are explicitly saying take the letters 2-4. What you could use is
word[1:]
This means slice it from the second letter till the end. Hope this helps!


#14

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