Wrute a python program: Count the number of 10-letter words in the file. http://aops-docs.s3.amazonaws.com/python1/wordlist.txt
OK, what did you come up with?
wordFile = open("wordlist.txt",'r') count = 1 for word in wordFile: if len(word) == 10: count += 1 print(count) wordFile.close()
The answer doesn't come out right! I don't know why @e.mccormick
why does count start at 1 and not 0?
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 :)
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.
Join our real-time social learning platform and learn together with your friends!