Hi, guys I'm having trouble writing this word count program in python3. I've been able to find alot of help for past versions but nothing for 3.

It is just a simple word count program, with a user input sentence.

So far I have

def main():
 
      print ("This program calculates the number of words in a sentence")
   

      print
   
      p =input("Enter a sentence: ")

      words = line.split(p)
   
      wordCount = len(words)
   
      print ("The total word count is:", wordCount)
   
main()

Recommended Answers

All 3 Replies

Two things:

  1. You should use the (CODE) button around your code: It saves indentation, does keyword coloring, adds line numbers. All good things
  2. What is this line doing? words = line.split(p).

I don't see any declaration of the variable line... and suspect you have cut and pasted something without completely finishing the fixup needed.

I suppose when he type

words = line.split(p)

he meant

words = p.split()

I suppose that if he doesn't respond after some days then he is not interested any more. (probably he solved his own problem and does not care to spend time at DaniWeb)

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.