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()
23 Answers
+ 5
last line doesn't have a newline character
+ 3
Friends Faiz
+ 2
Faiz AR , as it is mentioned in the task readlines method includes the newline character "\n". You should remove it.
+ 1
THAKKAR HEER see my comment above?
+ 1
THAKKAR HEER it means yes
+ 1
THAKKAR HEER are you using my code in the comments? Make sure to give proper indentation
+ 1
I erased the comment
+ 1
I have to use it after comments?
+ 1
Faiz ar its coming invalid syntax line 8
+ 1
Please help
+ 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
+ 1
Its ok there was my mistake
+ 1
Thanks
+ 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
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()
0
Is it now working
0
THAKKAR HEER yep
0
Yep means what
0
Ok thanks so much and sorry for disturbing
0
Faiz Ar i did the same way you did but still it is showing invalid syntax