+ 5

Python core Lesson 51

It works but I don't know why i should ad "-1" for all lines but not for last line file = open("/usercode/files/books.txt", "r") for f in file: if not (len(f) ==18): print (f[0] + str((len)(f)-1)) else: print (f[0] + str((len)(f))) file.close()

13th Sep 2021, 8:31 AM
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ - avatar
18 Answers
+ 10
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ , the code you are presenting is "hard-coded" as it only checks if length is a value of 18 what you can do: โ–ช๏ธŽin your code, the variable 'f' contains the respective string from the file in each iteration cycle of the for loop. all lines (exept the last ones) coming from the file are terminated with a newline- sequence. this can be removed from the string by using strip() like f.strip(). if there is no new-line sequence nothing happens. โ–ช๏ธŽstrip removes all kind of whitespace. this can be a simple space, as well as a new-line sequence. happy coding and good success!
13th Sep 2021, 9:45 AM
Lothar
Lothar - avatar
+ 9
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ , taking the code from Alex and do some changes: file = open("/usercode/files/books.txt", "r") for line in file: print(line[0] + str(len(line.strip()))) file.close()
13th Sep 2021, 11:52 AM
Lothar
Lothar - avatar
+ 4
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ , practicing is important to grow the coding skills. try to solve simple tasks in simple and clear code. after some time you may go back to your code and try to improve it. always keep in mind that a good readability is the key point for maintaining code. an other way to improve your ability is reading codes from other people, like in playground, or in the q&a section. take codes that people have posted and also raised a question to it. try to find a solution first without having a look what people have posted. then later on have a look at their solutions not to forget websites like geeksforgeeks, tutorialspoint and others. also see realpython, which have a lot of free tutorials. to get dedicated answers, stackoverflow is the one of the best sources. as reading in the web can sometimes be tiring, also have a look at videos from YouTube. try to find what suits you best happy coding and good success!
15th Sep 2021, 10:55 AM
Lothar
Lothar - avatar
+ 3
Each line has a newline character appended to it except last line(obviously)
13th Sep 2021, 8:34 AM
Abhay
Abhay - avatar
+ 2
Thank you! Now I get it!
13th Sep 2021, 11:14 AM
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ - avatar
+ 1
Abhay, How can I add last line for "else" if I don't know the lenght of this line for example?
13th Sep 2021, 8:36 AM
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ - avatar
+ 1
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ sorry buy i don't understand what you mean by not knowing the length of last line . Anyway ,Why do you need to know the length of last line ?
13th Sep 2021, 9:17 AM
Abhay
Abhay - avatar
+ 1
Lothar, Which line I should add strip()?
13th Sep 2021, 9:56 AM
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ - avatar
+ 1
Add strip to every line . rstrip() will remove white spaces(including newline) from the end of line .
13th Sep 2021, 10:01 AM
Abhay
Abhay - avatar
+ 1
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ you don't need to strip from the f[0] unless question says so. Strip from the one that you are getting length of. and honestly i won't even use strip since you don't know if the white space was deliberately put there or not. Just use rstrip.
13th Sep 2021, 11:11 AM
Abhay
Abhay - avatar
+ 1
file = open("/usercode/files/books.txt", "r") for i in file: new_list = i.strip() print(new_list[0] + str(len(new_list))) file.close()
13th Sep 2021, 11:27 AM
Alexander
+ 1
Lothar, Yes, i have already done like this Thanks for helping!
13th Sep 2021, 11:55 AM
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ - avatar
0
Abhay, When I do like this There are no any changes Last line still less than needed:( file = open("/usercode/files/books.txt", "r") for f in file: print (f[0].strip() + str(len(f))) file.close()
13th Sep 2021, 11:09 AM
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ - avatar
0
Lothar I started learning programming some days ago But I understand that SoloLearn's lessons isn't enough to do a lot of things Can You give an advice what i should to read to become more skillful? Sorry for disturbing you
13th Sep 2021, 12:05 PM
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ
ะœะฐั‚ะฒะตะน ะกั‚ะฐัะตะปัŒะบะพ - avatar
0
I actually don't know what is happening help me out
15th Sep 2021, 8:34 AM
Vitu Mhango
Vitu Mhango - avatar
0
file = open("/usercode/files/books.txt", "r") #your code goes here for line in file: print(line[0]+ str(len(line.strip("\n")))) file.close()
14th Oct 2021, 10:22 AM
George Zirbo
George Zirbo - avatar
0
Yes, this answer works but will not work if the list is ever added to. Up to this point, SoloLearn has not taught the strip() function. Would there be a way to do this code project without it?
14th May 2022, 3:10 PM
Brendan Maxwell
0
Here is my code: file = open("/usercode/files/books.txt", "r") #List of books books = file.readlines() #print(books) #Qty of rows qty_rows = len(books) #print(qty_rows) #First letter of book / first char in each row #for i in range(qty_rows): # print(books[i][0]) #Qty chars in each row #for i in range(qty_rows): # row = books[i] # l_row = len(row) # print(l_row) for i in range(qty_rows): row = books[i] l_row = len(row) if i != qty_rows-1: print(books[i][0] + str(l_row - 1)) else: print(books[i][0] + str(l_row)) file.close() Is it OK?
7th Jul 2022, 6:33 PM
Pavel Ripyuk
Pavel Ripyuk - avatar