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