0

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()

Edited by Reverend Jim: Corrected code formatting

8
Contributors
12
Replies
51
Views
6 Years
Discussion Span
Last Post by Peter_38
Featured Replies
  • You have some mistake,and use code tag. See if this help. [CODE]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()[/CODE] Read More

0

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

1

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()
0

Another flavour....;)

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

Edited by richieking: n/a

0
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.

1

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.

0

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.

0
    # - *- 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

Edited by Peter_38

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.