+ 1

Why is the code i wrote for 'Book Titles' failing?

I can't pass the 4th project even my output is correct. Could you please explain why? file = open("/usercode/files/books.txt", "r") x = file.readlines() y = len(x) for i in range(y): A = x[i][0] B = x[i] z = len(B) - 1 print(A,z, sep="") file.close()

25th Oct 2020, 11:41 AM
Berk
Berk - avatar
3 Respuestas
+ 3
Last line doesn't contain '\n'. So subtract 1 from each line except the last one.
25th Oct 2020, 11:43 AM
QTWizard
+ 3
Berk , as QTWizard already mentioned, is the problem the trailing new-line sequence. Instead of doing some weird slicing, you can strip off the new-line sequence by using the string method strip(), which removes all kind of whitespaces from the left and right side of a string.
25th Oct 2020, 2:51 PM
Lothar
Lothar - avatar
+ 1
Thank you QTWizard , Lothar i got it now.
25th Oct 2020, 3:59 PM
Berk
Berk - avatar