Hello guys!!
I really need your help, I'm starting to use python and I have to write a program that counts the letters of a sentence entered by the user. This is what I have so far, but I don't know why it is not working.. Please help, thank you

def main():
    phrase = input("Enter a sentence:")
    words = phrase.split
    wordCount = len(words)
    print("The total word count is:")
main()

Recommended Answers

All 14 Replies

Value of words will be the method for splitting strings. Maybe that is not what you want.

You have some mistake,and use code tag.
See if this help.

def main():
    phrase = input("Enter a sentence:")
    words = phrase.split() #Forget ()
    wordCount = len(words)
    print("The total word count is: %s" % wordCount) #You have to include wordCount in print
main()

Another flavour....;)

def main():
    print len([x for x in raw_input("enter a phrase:" ).split()])
main()
def main():
    print len([x for x in raw_input("enter a phrase:" ).split()])
main()

Just FYI: I receive a SyntaxError: invalid syntax when I tested this code in Python 3.1.3.

The function named "raw_input" in Python 2.x is named "input" in Python 3.x . Since you did not specify what version of Python you use (until your last post), people were naturally confused. More so since many many more Python 2.x users than 3.x.

raw_input became input in python 3. You can use 2to3.py script to do most changes for you.

Hope you got it now... ;)

Hope I didn't confuse you Fo.katia. I just wanted to point out that there are some difference between Python versions 2 & 3 that we new guys could get lost on.

well if you are happy.... Then mark the thresd as solved.
:)

what is import collections with examples

Bold Text Herewhat is import collections ,import itme.getter

    # - *- coding: utf- 8 - *-
    def words(string):
        wordstr = string.split() 
        wordict = {} # dictionary to holds the word against count
        for word in wordstr:
            if word in wordict:
                wordict[word] += 1
            else:
                wordict[word] = 1
        return wordict

Here is an update

def main():
    phrase = input("Enter a sentence:")
    wordcount = len(phrase)
    print("The total word count is: " + str(wordcount))
main()

Please don't post code without any testing it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.