+ 1

Help me out please

I was trying out a code project and I wasn't getting the desired output. I've come close to the output and I really don't know what's wrong with my code. Please someone should help me out Here's the question: 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

4th Jul 2022, 11:23 AM
Emmanuel Okechukwu
Emmanuel Okechukwu - avatar
3 Réponses
+ 2
Emmanuel Okechukwu :, here is a description and some hints: - open the file for reading (code already given from sololearn) - use the file object from the previous step and the method readlines(), store the result in a variable like 'lines' - iterate through 'lines' - for each line you will get a string. - take the first letter of the string and store it in a variable like 'char' - take the length of the complete string of each line in a variable like 'length' - hint: the string contains a trailing 'new line sequence', that does count for the string length -> remove this 'new line sequence', have a look at the string method strip() that may help you -> the last line of the file does NOT have this trailing 'new line sequence' - concatenate the first letter (variable 'char')and the correct value (variable 'length') of the string length - output the combined string for the current line - close the file
4th Jul 2022, 2:01 PM
Lothar
Lothar - avatar
+ 1
Are you able to read the titles from the file and assign them to a variable? After that I would just do title.replace(“ “, ““) to remove all spaces… You can do len(title) to get the length… to get the first character do title.split() and the first character will be title[0]…
4th Jul 2022, 11:30 AM
Janne
Janne - avatar
+ 1
Emmanuel Okechukwu it is really hard for others to debug your code without seeing it. Could you paste a copy here? Better yet, paste it into Code Playground and post a link to it.
4th Jul 2022, 7:35 PM
Brian
Brian - avatar