Python3 Book titles problem | Sololearn: Learn to code for FREE!

+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()

bug

10/26/2020 7:39:26 PM

Pascal Nicolas ROBERT

9 Answers

New Answer

+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

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()

+1

#############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()

0

Sorry but the link is not available

0

My solution on Python 3 Chapter 5 Book Titles Challenge https://code.sololearn.com/cfX9h3Fu4C8Q/#py

-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)))

-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()

-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()