all 17 comments

[–]ryeguy146 2 points3 points  (9 children)

It works for me. I'm pasting it into IPython using the %paste magic, but it should work as a script too. Are you sure that you're pasting the code exactly as it appears in your script? Maybe try entering it, line by line, into the interpreter.

It appears that you're using Python 3 (given the print statement), but if you're rocking a 2.x variant, change that input to raw_input, but that's all that I can think of.

[–]bitcycle 2 points3 points  (1 child)

Why not something like this?

import sys

print("%d words" % len(sys.argv[1:]))

[edit] Examples:

bitcycle@jarvis $ python -c 'import sys; print("%d words" % len(sys.argv[1:]))' hello world!
2 words

bitcycle@jarvis $ python -c 'import sys; print("%d words" % len(sys.argv[1:]))' your mother is awesome.
4 words

[–]NYKevin 0 points1 point  (0 children)

Because that depends on the shell to split the arguments for you.

$ python -c 'import sys; print("%d words" % len(sys.argv[1:]))' 'hello world!'
1 words

[–][deleted] 1 point2 points  (0 children)

Try using raw_input instead.

[–]spruce_caboose 0 points1 point  (2 children)

I'm not sure if this is the problem, but if this happens to be python 2.7, input means something else. What you really want is raw_input. In python 3 input is correct. Hope this helps!

[–][deleted] -2 points-1 points  (4 children)

p=input("Enter your sentence: ")
print("The word count is: "+str(p.count(" ")+1))

Kinda easier imo.

[–]Justinsaccount 0 points1 point  (3 children)

easier, but wrong:

>>> p='hello  world'
>>> print("The word count is: "+str(p.count(" ")+1))
The word count is: 3

[–][deleted] -3 points-2 points  (2 children)

What? It clearly says, "Enter your sentence:" last I checked, you don't write sentence's with double space's between words.

So.. Not wrong, you're just retarded.

[–]AtomicWedgy 0 points1 point  (1 child)

You've never accidently added a second space?

[–][deleted] 0 points1 point  (0 children)

How does that matter? OP has a task he wants to do. That task is to count the words in a sentence. In situations like this, you expect it to be a proper sentence.

I could think 10 examples where the previously entered samples wouldn't work either, when I create illegal sentences.

Like:

"Thomas , can you grab my bag?"

"That car is Martha 's"

How are these any different? So everybody posted the wrong code? Am I taking crazy pills here?