0

Can someone help me figure out what I'm doing wrong with the code I put in the description?

file = open("/usercode/files/books.txt", "r") #your code goes here for i in file: i+=1 title=file.read([i]) letter=str(title[0]) num=str(len(title)) print(letter+num) file.close()

17th Jan 2021, 3:25 PM
Michele Rene Machado
Michele Rene Machado - avatar
1 Réponse
+ 2
( 1 ) You don't need the i+=1 as the "i" represent string or the line each file so adding 1 to it will cause an error. ( 2 ) You also don't need the title=file.read(i) because technically the program is already reading the file by iterating its each line using "for loop". And this will also cause an error because "i" is actually a string and file.read(integer) is its required parameter. ( 3 ) The "print" should be indented inside the "for loop" as the problem requires to print each first letter and length of each line in the file. And additional, you can use i.strip() to remove "\n" or newlines every string so that the program will only find the length of the string without an extra \n. ( Please update me if it still not solved, cause I'm using web version, which currently has bugs in mine. And if my explanation is still unclear, please feel free to ask. Thanks! ) https://code.sololearn.com/ca12a22a6a1a
17th Jan 2021, 3:54 PM
noteve
noteve - avatar