Book title project | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Book title project

Hi.iam learning Python recently.can anyone tell me what the problem is with this code?:( file = open("/usercode/files/books.txt", "r") list=file.readline() for i in list: if i==list[len(list)-1]: title=i[0]+str(len(i)) print(title) else: title=i[0]+str(len(i)-2) print(title) file.close()

15th Jan 2021, 6:30 PM
Marjaf
Marjaf - avatar
19 Answers
+ 10
Or, instead of checking whether there is a "\n" at the end of each line, you could just get rid of them 😉 with open("/usercode/files/books.txt") as file: for line in file: print(line[0] + str(len(line.strip("\n"))))
16th Jan 2021, 3:51 AM
David Ashton
David Ashton - avatar
+ 6
file = open("/usercode/files/books.txt", "r") list=file.readlines() for line in list : if "\n" in line: print(line[0]+str(len(line)-1)) else: print(line[0]+str(len(line))) file.close() This should work In the first if you replace it with an if \n in i: (you search if there is a new line) Then in the next line (title=i[0]...) you need to subract 1 from the number of charcters because else it would count \n And in the else: you don't need that - 2 The rest should be fine Hope it's helpfull
15th Jan 2021, 7:13 PM
_jakubwrz_
_jakubwrz_ - avatar
+ 4
Or just 3 lines with open("/usercode/files/books.txt") as file: for title in file.readlines(): print(title[0] + str(len(title.strip("\n"))))
17th Feb 2021, 6:20 AM
David Ashton
David Ashton - avatar
+ 3
Mr Myth thank you🙏
15th Jan 2021, 8:36 PM
Marjaf
Marjaf - avatar
+ 2
_jakubwrz_ I seeeeeee😅😂 I forgot this...I wrote wrong readline instead readlines😐😅 And in else, I had to write -1 instead -2. It works.
15th Jan 2021, 8:14 PM
Marjaf
Marjaf - avatar
15th Jan 2021, 9:43 PM
JaScript
JaScript - avatar
+ 2
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
16th Jan 2021, 10:17 AM
Lam Wei Li
Lam Wei Li - avatar
+ 1
Oh really? I tried it before i posted it, but I will try again
15th Jan 2021, 7:56 PM
_jakubwrz_
_jakubwrz_ - avatar
+ 1
Just change list=file.readline() to list=file.readlines()
15th Jan 2021, 8:21 PM
Mohammad Hejazi
Mohammad Hejazi - avatar
+ 1
Also you can use .strip() for remove whitespace
15th Jan 2021, 8:23 PM
Mohammad Hejazi
Mohammad Hejazi - avatar
+ 1
I tested the code again, it works for me... idk \n take only - 1
15th Jan 2021, 8:42 PM
_jakubwrz_
_jakubwrz_ - avatar
+ 1
file = open("/usercode/files/books.txt","r") rows = file.readlines() pit = len(rows) i = 0 while i < pit: name = rows[i] title = name.strip("\n") lenght = str(len(title)) answer = title[0] + lenght print(answer) i += 1 file.close()
1st Mar 2022, 5:02 PM
Stilakoni
0
file = open("/usercode/files/books.txt", "r") #your code goes here books=file.readlines() for i in books: if i==books[-1]: value=len(i) else: value=len(i)-1 print(i[0]+str(value)) file.close()
25th Apr 2021, 10:29 AM
A Owadud Bhuiyan
A Owadud Bhuiyan - avatar
- 1
_jakubwrz_ Thank you but it doesn't work:(
15th Jan 2021, 7:43 PM
Marjaf
Marjaf - avatar
- 1
file = open("/usercode/files/books.txt", "r") for line in file: print(line[0]+str(len(line.strip("\n")))) file.close() You add .strip("\n") after line so it does not count \n as character when you are counting the length
8th Jun 2021, 7:06 PM
Jacques Savary Benjy Adolphe
Jacques Savary Benjy Adolphe - avatar
- 1
Here this worked for me file = open("/usercode/files/books.txt", "r") #your code goes here f = file.readlines() for i in f: x = i[0] if '\n' in i: y = len(i)-1 else: y = len(i) print (x + str(y)) file.close()
30th Mar 2022, 6:58 PM
KingDavid Chibueze Ezennwa
KingDavid Chibueze Ezennwa - avatar
- 1
This is my solution: file = open("/usercode/files/books.txt", "r") #your code goes here name = file.readlines() var = 1 while (-len(name)+var) <= 0: num = int (-len(name)+var)-1 s = name[num].split(" ") cont = "".join(i[0] for i in s) print (cont) var+=1 file.close()
22nd Jul 2022, 3:45 PM
Ruben
- 1
file = open("/usercode/files/books.txt", "r") for line in file: words = line.split() i = "" for word in words: i += word[0] print(i) file.close()
22nd Dec 2022, 5:49 AM
Juan David Ortiz Guevara
Juan David Ortiz Guevara - avatar
- 1
file = open("/usercode/files/books.txt", "r") for line in file: words = line.split() i = "" for word in words: i += word[0] print(i) file.close()
22nd Dec 2022, 5:49 AM
Juan David Ortiz Guevara
Juan David Ortiz Guevara - avatar