0

Please help cause i dont understand how this is

Well i have this test from python 3: _Book Titles: _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. And this is my code: file = open("/usercode/files/books.txt", "r") line=file.readlines() for line in line: print(line[0]+str(len(line))) file.close() ok,so the output should be: _H12 _T16 _P19 _G18 And mine is: H13 T17 P20 G18 If anyone can help me with this i would be very thankfull,because im new with python and i think i were doing it well but i cant with this.

18th Jan 2021, 10:21 PM
Melissa Franco
Melissa Franco - avatar
9 Respostas
0
yeah... two special chars in each line and one in the last line you did well. simply 2 less and one less for last entry.
18th Jan 2021, 10:31 PM
Oma Falk
Oma Falk - avatar
+ 3
You can try this: list=[] for i in file: list.append(i.strip("\n")) for i in list: print(i[0] + str(len(i))) Your Output: H12 T16 P19 G18 Expected Output: H12 T16 P19 G18
21st Jan 2022, 10:28 AM
Hritika Bhagat
Hritika Bhagat - avatar
+ 1
So, I had the exact same problem as Melissa, and while the other answers I saw here worked to arrive at a solution that fit the problem, they used techniques that had not been taught to us at that point in the SoloLearn course. We hadn't learned the replace or strip commands (at least not to my knowledge) at that point, soI kept playing around with the problem, and I finally came up with this answer for my code: file = open("/usercode/files/books.txt", "r") book_titles = file.readlines() for title in book_titles: title_letter = title[0] title_length = str(len(title) - 1) if title_length == "17": title_length = "18" print(title_letter + title_length) file.close() I know this wouldn't work if other titles with a length of 17 (after you subtract 1 from the initial length) had been on the list, but since the last book in this list was the only one with that length, this code got the job done.
1st Jul 2022, 1:13 AM
David Hickey
David Hickey - avatar
+ 1
file = open("/usercode/files/books.txt", "r") list = file.readlines() for line in list: x = line.strip() print(line[0] + str(len(x))) file.close() Try this code, bro. Here, the strip() function removes the space(\n) between lines. The new line is stored in x. Now, the len() function counts the new line.
13th Nov 2022, 5:32 PM
vivek ravi
vivek ravi - avatar
0
There is a hint at the end of the problem description which tackles your problem. You even pasted it. All lines but the last end end with a newline character (\n)
18th Jan 2021, 10:37 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Ok i'll try the way i understood. Yeah,i read it before but i dont know how to erase it. Thank you both
18th Jan 2021, 11:35 PM
Melissa Franco
Melissa Franco - avatar
0
Ok so i did this: file = open("/usercode/files/books.txt", "r") line=file.readlines() for line in line: print(line[0]+str(len(line)-1)) file.close() And the output is: H12 T16 P19 G17 I tried to use lists,arrays,to make that G17 has 1 more,but it shows me error,think because it cant be done.
19th Jan 2021, 12:00 AM
Melissa Franco
Melissa Franco - avatar
0
hello there, file = open("/usercode/files/books.txt", "r") #your code goes here list = [] for i in file: list.append(i.replace("\n", "")) for x in list: print(x[0] + (str(len(x)))) file.close()
21st May 2022, 5:52 PM
Wilmer Barahona
0
I used if and else with for loop. It works. Here is the code: file = open("/usercode/files/books.txt", "r") #your code goes here books = file.readlines() for title in books: if title != books[-1]: print(title[0] + str(len(title)-1)) else: print(title[0] + str(len(title))) file.close()
25th Feb 2023, 9:09 PM
Wally