0

Book Titles Question

Can anyone tell me what is wrong with my code? I only have errors on the last part in which it should print G18 instead of G17 file = open("/usercode/files/books.txt", "r") mylist =[] i=0 for word in file: mylist.append(word) first_letter = mylist[i][0] no_char = str(len(mylist[i])-1) i+=1 file.close()

20th Nov 2020, 12:42 PM
Faiz AR
Faiz AR - avatar
23 Respostas
+ 5
last line doesn't have a newline character
20th Nov 2020, 12:45 PM
Abhay
Abhay - avatar
+ 3
Friends Faiz
22nd Nov 2020, 8:25 AM
Thakkar Heer
Thakkar Heer - avatar
+ 2
Faiz AR , as it is mentioned in the task readlines method includes the newline character "\n". You should remove it.
20th Nov 2020, 12:45 PM
TheWhĀ”teCat šŸ‡§šŸ‡¬
TheWhĀ”teCat šŸ‡§šŸ‡¬ - avatar
+ 1
THAKKAR HEER see my comment above?
22nd Nov 2020, 8:23 AM
Faiz AR
Faiz AR - avatar
+ 1
THAKKAR HEER it means yes
22nd Nov 2020, 8:24 AM
Faiz AR
Faiz AR - avatar
+ 1
THAKKAR HEER are you using my code in the comments? Make sure to give proper indentation
22nd Nov 2020, 8:56 AM
Faiz AR
Faiz AR - avatar
+ 1
I erased the comment
22nd Nov 2020, 8:57 AM
Thakkar Heer
Thakkar Heer - avatar
+ 1
I have to use it after comments?
22nd Nov 2020, 8:57 AM
Thakkar Heer
Thakkar Heer - avatar
+ 1
Faiz ar its coming invalid syntax line 8
22nd Nov 2020, 9:38 AM
Thakkar Heer
Thakkar Heer - avatar
+ 1
Please help
22nd Nov 2020, 9:39 AM
Thakkar Heer
Thakkar Heer - avatar
+ 1
THAKKAR HEER i dont really know what to say. I also tried doing the problem again using my comment code and it works fine
22nd Nov 2020, 9:55 AM
Faiz AR
Faiz AR - avatar
+ 1
Its ok there was my mistake
22nd Nov 2020, 10:45 AM
Thakkar Heer
Thakkar Heer - avatar
+ 1
Thanks
22nd Nov 2020, 10:45 AM
Thakkar Heer
Thakkar Heer - avatar
+ 1
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:38 PM
Lam Wei Li
Lam Wei Li - avatar
0
Fixed it. file = open("/usercode/files/books.txt", "r") mylist =[] i=0 for word in file: mylist.append(word) for x in mylist: first_letter = mylist[i][0] if i + 1 == len(mylist): no_char = str(len(mylist[i])) else: no_char = str(len(mylist[i])-1) print(first_letter+no_char) i+=1 file.close()
20th Nov 2020, 12:55 PM
Faiz AR
Faiz AR - avatar
0
Is it now working
22nd Nov 2020, 8:11 AM
Thakkar Heer
Thakkar Heer - avatar
22nd Nov 2020, 8:21 AM
Faiz AR
Faiz AR - avatar
0
Yep means what
22nd Nov 2020, 8:21 AM
Thakkar Heer
Thakkar Heer - avatar
0
Ok thanks so much and sorry for disturbing
22nd Nov 2020, 8:25 AM
Thakkar Heer
Thakkar Heer - avatar
0
Faiz Ar i did the same way you did but still it is showing invalid syntax
22nd Nov 2020, 8:54 AM
Thakkar Heer
Thakkar Heer - avatar