Simpler way? [SOLVED]
I just completed a code coach problem but I feel like I am over complicating it. Is there a simpler way to accomplish the task than how I have? PROBLEM: You have been asked to make a special book categorization program, which assigns each book a special code based on its title. The code is equal to the first letter of the book, followed by the number of characters in the title. For example, for the book "Harry Potter", the code would be: H12, as it contains 12 characters (including the space). You are provided a books.txt file, which includes the book titles, each one written on a separate line. Read the title one by one and output the code for each book on a separate line. For example, if the books.txt file contains: Some book Another book Your program should output: S9 A12 Recall the readlines() method, which returns a list containing the lines of the file. Also, remember that all lines, except the last one, contain a \n at the end, which should not be included in the character count. MY SOLUTION: file = open("/usercode/files/books.txt", "r") #your code goes here fileLines = [] for lines in file: fileLines.append(lines) length = 1 for n in fileLines: if length == len(fileLines): print(n[0] + str(len(n))) else: print(n[0] + str(len(n)-1)) length += 1 file.close() I am using len(n) - 1 because there is an extra space (“ “) on all of the lines except for the last line. I opted not to use the readlines() method, but maybe I should have? Thank you for the help!