0

help in book title project

hello everyone ! I've been trying for an hour to solve this question And I get a headache this is the question : https://www.sololearn.com/learning/eom-project/1073/353 I was having a problem with the last value later I found out that the last line is not there \n so i use if conditon now i have a problem in all except last one hahaha how can i fix it ? https://code.sololearn.com/cvl8B59a6Jyx

25th Aug 2021, 9:05 PM
Mjd Almalti
5 ответов
+ 1
@➵Chloe[she/her] yeah there is no bug , its logical error the output should be H12 T16 P19 G18 and code give H13 T17 P20 G18
25th Aug 2021, 10:29 PM
Mjd Almalti
+ 1
file = open("/usercode/files/books.txt", "r") for i in file: if i.endswith("\n"): length=len(i[0:i.index('\n')]) else: length=len(i[0:]) slice=i[0:1] print(slice,end="") print(length) file.close() print("\n") #Other way to do it with open("/usercode/files/books.txt","r") as f: for i in f: print(i[0]+str(len(i.rstrip())))
25th Aug 2021, 10:35 PM
Abhay
Abhay - avatar
0
Mjd Almalti How about this one? :- with open("/usercode/files/books.txt", "r") as file: file = file.read().split("\n") for x in file: print(x[0] + str(len(x)))
26th Aug 2021, 6:22 AM
Calvin Thomas
Calvin Thomas - avatar
0
thank you all I know there are many easier ways to implement this code, but I am educated at this stage and I would like to use the hard ways that I used at the beginning of thinking and then compare them with the easier ways I just want to know where the error was in the code I wrote, so I can learn from my mistake
26th Aug 2021, 7:09 AM
Mjd Almalti
0
for i in file: if i.endswith("\n"): length=len(i[0:i.index('\n')]) else: length=len(i[0:]) slice=i[0:1] print(slice,end="") print(length)
31st May 2022, 6:54 PM
Archana Aarya
Archana Aarya - avatar