Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

Wrute a python program: Count the number of 10-letter words in the file. http://aops-docs.s3.amazonaws.com/python1/wordlist.txt

OpenStudy (e.mccormick):

OK, what did you come up with?

OpenStudy (anonymous):

wordFile = open("wordlist.txt",'r') count = 1 for word in wordFile: if len(word) == 10: count += 1 print(count) wordFile.close()

OpenStudy (anonymous):

The answer doesn't come out right! I don't know why @e.mccormick

OpenStudy (bibby):

why does count start at 1 and not 0?

OpenStudy (anonymous):

You need to trim spaces and white space characters that len() function counts. basically replace if len(word) == 10 with if len(word.strip()) == 10 and you'd need to start count from 0 as @bibby mentioned :)

OpenStudy (anonymous):

also, the more "pythonic" way of openning a file is using with open(..) as filehandler: construct. This way you don't need to close it too.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!