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 + str((len)(f)-1)) else: print (f + str((len)(f))) file.close()
9/13/2021 8:31:42 AMМатвей Стаселько
16 AnswersNew Answer
Матвей Стаселько , 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!
Матвей Стаселько , taking the code from Alex and do some changes: file = open("/usercode/files/books.txt", "r") for line in file: print(line + str(len(line.strip()))) file.close()
Матвей Стаселько , 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!
Each line has a newline character appended to it except last line(obviously)
Thank you! Now I get it!
Abhay, How can I add last line for "else" if I don't know the lenght of this line for example?
Матвей Стаселько 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 ?
Lothar, Which line I should add strip()?
Add strip to every line . rstrip() will remove white spaces(including newline) from the end of line .
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.strip() + str(len(f))) file.close()
Матвей Стаселько you don't need to strip from the f 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.
file = open("/usercode/files/books.txt", "r") for i in file: new_list = i.strip() print(new_list + str(len(new_list))) file.close()
Lothar, Yes, i have already done like this Thanks for helping!
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
I actually don't know what is happening help me out
file = open("/usercode/files/books.txt", "r") #your code goes here for line in file: print(line+ str(len(line.strip("\n")))) file.close()