- 1
Hello, please somebody help me how to solve " book titles " problem in ' exception & files ' in python
I don't know how to code it ?
4 Respostas
+ 3
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
+ 1
use file.readlines() and then iterate over it ,checking if each line contains "\n" if yes then replace that with empty string ("") and then print line[0]+str(len(line))
0
Maybe will be help the repeating of this part.
https://code.sololearn.com/cPeBWF4tkD8w/?ref=app
0
file = open("/usercode/files/books.txt", "r")
#your code goes here
books=file.readlines()
for i in books:
if i==books[-1]:
value=len(i)
else:
value=len(i)-1
print(i[0]+str(value))
file.close()