Hello! I have to do this task, but I don’t know where to start neither how to do it, could you help me please, Thank you! 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 https://code.sololearn.com/cTFg60GR2X3I/?ref=app
11/2/2020 7:13:08 PMAlejandra b
6 AnswersNew Answer
A simpler way with 5 lines of code. A simple explanation, loop through all lines, and if a line has trailing/ending "\n" remove it from the count of the length. https://code.sololearn.com/cdaPdMWWeN3W/?ref=app
https://code.sololearn.com/cV44Du4ugn0g/?ref=app Check this
file = open("/usercode/files/books.txt", "r") for line in file: print(line+str(len(line.strip("\n")))) file.close() You add .strip("\n") after line so it does not count \n as character when you are counting the length
You can see output is a list of values.. Read lines in a loop and print first letter and print length of string...
Another simple way to do it 😅 https://code.sololearn.com/cML3C1NNbynn/?ref=app