Python3 Book titles problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python3 Book titles problem

Hi i have good answers but it's write fail with this code why? file = open("/usercode/files/books.txt", "r") #your code goes here books=file.readlines() for title in books: code=title[0]+str(len(title)-1) print(str(code)) file.close()

26th Oct 2020, 7:39 PM
Pascal Nicolas ROBERT
Pascal Nicolas ROBERT - avatar
9 Answers
+ 4
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
15th Dec 2020, 6:39 PM
Lam Wei Li
Lam Wei Li - avatar
+ 3
#############My version ############### file = open("/usercode/files/books.txt", "r") books = file.readlines() t1=books[0] print(t1[0]+str(len(books[0])-1)) t2=books[1] print(t2[0]+str(len(books[1])-1)) t3=books[2] print(t3[0]+str(len(books[2])-1)) t4=books[3] print(t4[0]+str(len(books[3]))) file.close()
15th Aug 2021, 6:19 PM
Hamdambek🖥️🎧
Hamdambek🖥️🎧 - avatar
0
Sorry but the link is not available
27th Oct 2020, 5:27 PM
Pascal Nicolas ROBERT
Pascal Nicolas ROBERT - avatar
0
My solution on Python 3 Chapter 5 Book Titles Challenge https://code.sololearn.com/cfX9h3Fu4C8Q/#py
7th Nov 2020, 7:51 AM
Thet Tun Oo
Thet Tun Oo - avatar
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()
25th Apr 2021, 10:30 AM
A Owadud Bhuiyan
A Owadud Bhuiyan - avatar
- 1
with open("/usercode/files/books.txt", "r") as a: # reads each line b = a.readlines() for i in b: if i[-1] == "\n": print (i[0] + str(len(i)-1)) else: print (i[0] + str(len(i)))
13th Sep 2021, 5:33 PM
Kailash
Kailash - avatar
- 1
file = open("/usercode/files/books.txt", "r") titles = file.readlines() for title in titles: title = title.strip() print ("{}{}".format(title[0],len(title))) file.close()
27th Aug 2022, 6:21 AM
Andrii Levchenko
Andrii Levchenko - avatar
- 2
file = open("/usercode/files/books.txt", "r") a=file.readlines() for i in range(len(a)): if i==len(a)-1: print(str(a[i][0]) + str(len(a[i]))) else: print(str(a[i][0]) + str(len(a[i])-1)) file.close()
12th Jun 2021, 2:40 PM
Kiran Joshi
Kiran Joshi - avatar
- 2
f = open("/usercode/files/books.txt", "r") b = f.read() x=b.split("\n") for i in x: l=len(i) c=i[0] print(c+str(l)) f.close()
17th Jul 2021, 8:44 AM
Smriti Sawhney
Smriti Sawhney - avatar