0

Does anyone know how to solve this?

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 file = open("/usercode/files/books.txt", "r") #your code goes here file.close()

17th Jan 2021, 6:15 AM
Aurora
Aurora - avatar
6 Answers
+ 4
Please show your attempt first so we may help. We cannot just give you the solution as we believe it would prevent you from learning. Review your lessons and try to attempt it. Thanks for understanding!
17th Jan 2021, 6:21 AM
noteve
noteve - avatar
+ 2
It is because there is an escape sequence or newline "\n" as the last character of every string, or every line. You can use .strip() string method to remove those. string.strip() The rest is up to you. If there are still bugs/errors, feel free show us your attempt. Thanks!
17th Jan 2021, 6:41 AM
noteve
noteve - avatar
+ 1
finally i did it. file = open("/usercode/files/books.txt", "r") a = 0 #your code goes here for i in file: if str(a) <= str(len(i)): print(i[0]+str(len(i)-1)) else: print(i[0]+str(len(i))) a+=1 file.close()
17th Jan 2021, 10:23 AM
Aurora
Aurora - avatar
+ 1
Aurora That's another solution cause the last line does not have "\n". And by using if-else statement. Nice! Glad you solved it, Great work!👍
17th Jan 2021, 10:46 AM
noteve
noteve - avatar
0
file = open("/usercode/files/books.txt", "r") #your code goes here for i in file: print(i[0]+str(len(i))) file.close() This is my answer. But it is wrong.
17th Jan 2021, 6:26 AM
Aurora
Aurora - avatar
0
The answer should be H12 T16 P19 G18 But my answer is H13 T16 P19 G18
17th Jan 2021, 6:29 AM
Aurora
Aurora - avatar