1

I was wondering if anybody could help; I'm quite new to python.

I'm currently creating a tool which analyses the text inputted by a user and shows which feedback to which list that phrase belongs to.

So far the programme is on an infinite loop and counts how many expressions have been entered all together and then how many times something has occurred in a certain list.

if text in access:
    accessno +=1
    counter +=1
    print ('This could be classed as speech act 1: Access')
    print ("number of access hits ", accessno)
    print ("number of total hits ", counter)

So my question is this: how does one also get the programme to count how many words are in a sentence inputted by the user?

Any help would be much appreciated!

3
  • If you feel any of the answers below have provided you with a solution, please mark it as accepted. Commented Apr 7, 2013 at 13:57
  • It wont allow me due to being new on here! but thank you, you have helped a lot! :) Commented Apr 7, 2013 at 14:08
  • You can mark the answers on your own question as accepted, on the top left of the answer near the numbers. Your edit after the question has been solved changes your question too much, I will revert it to its original state, comments should be made as comments, not in your question, if you MUST include it in your question, add it on the bottom as an edit without changing the original meaning. Commented Apr 7, 2013 at 14:09

2 Answers 2

3

You can do it in the following simple way.

s = input()
# input() is a function that gets input from the user
len(s.split())
# len() checks the length of a list, s.split() splits the users input into a word list.

Links:

input() len() split()


Example:

>>> s = input()
"hello world"
>>> s
'hello world'
>>> s.split()
['hello', 'world']
>>> len(s.split())
2

Bonus: Do it all in one line!

print('You wrote {} words!'.format(len(input("Enter some text, I will tell you how many words you wrote!: ").split())))
Sign up to request clarification or add additional context in comments.

6 Comments

Looks like the OP is using Python 3 (print is a function in the given code, not a statement), so you would use input() instead.
Ah that simple! where ive got the user entering the code it currently is print ('Hello, Welcome to the chat log assistant') text = input('Please enter your text:') Yes its Python 3
@Volatility You can never know for sure unless OP confirms it, it's still valid Python 2 code, however OP does most likely use Python 3
input() is better as raw_input(): it shows that a string is expected (input() interprets strings). Also, input("") is more simply and naturally written as input(), since there is no input prompt (and is better as raw_input()).
@InbarRose: My bad, you're right, I had missed the Python 3 tag. +1.
|
-1
name = input ()
print len(name)

3 Comments

This will give the length of the entire string, not the number of words.
Omg, god I need to go to bed.
This works a treat: print ('Hello, Welcome to the chat log assistant') text = input('Please enter your text:') len(text.split()) print ("You have entered " + text ) if text in access: print ("total number of words" ,len(text.split())) Cheers guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.