0

Solution book titles

I don't able write code for this practice . Anyone help me

22nd Jul 2021, 3:49 PM
Kamal
Kamal - avatar
6 odpowiedzi
+ 8
Thanks I solved it file = open("/usercode/files/books.txt", "r") for word in file.readlines(): if "\n" in word: print (word[0]+str(len(word)-1)) else : print (word[0]+str(len(word))) file.close()
22nd Jul 2021, 4:27 PM
Kamal
Kamal - avatar
+ 1
with open("/usercode/files/books.txt", "r") as f: for line in f: line=line.rstrip() print(line[0]+str(len(line)))
18th Oct 2022, 10:04 AM
Jabar Hamza
Jabar Hamza - avatar
+ 1
file = open("/usercode/files/books.txt", "r") #your code goes here a=file.readlines() for i in a: b=i.replace("\n","") print(i[0] + str(len(b))) file. close() Mine looks different, 😅 hope it will help u
21st Jan 2023, 6:04 AM
Manoj Babu Panem
0
Where's your attempted code? Post them here
22nd Jul 2021, 3:53 PM
RKK
RKK - avatar
0
thank you very much
11th Jan 2022, 11:26 AM
YAO DABARA MICKAEL
YAO DABARA MICKAEL - avatar
- 2
This would another way to solve it, if you don´t realise question "in" at if statement. First read lines to count them, after read them again making condition, controls end line respect first lines file = open("/usercode/files/books.txt", "r") #your code goes here cont = 0 for i in file: cont+=1 file.close() lines = open("/usercode/files/books.txt", "r") i = 0 for j in lines: i+=1 if i != cont: print( j[0] + str(len( j ) - 1 )) else: print( j[0] + str(len( j ))) lines.close()
21st Mar 2022, 12:18 AM
ivan gonzalez
ivan gonzalez - avatar