Working with Files module in Intermediate Python
You are given a books txt file, which includes book titles, each on a separate line. Create a program to output how many words each title contains in the following format: Line 1: 3 words Line 2: 5 words Make sure to match the above mentioned format in the output. To count the number of words in a given string, you can use the split() function, or, alternatively, count the number of spaces (for example, if a string contains 2 spaces, then it contains 3 words). Note: With each re-write of this project, the code got worse. At first, there's no input for the answer and the format appeared to be the same as the expected output but it was wrong. with open("/usercode/files/books.txt") as f: i = 1 for i in f: print(f"Line"+ i+": "+str(len(i.split()))+" words") All of the output is on one line and the variable i, since it is in f, is string instead of 1, 2, 3, etc. Getting book titles instead of line numbers is a first for me.