15 AnswersNew Answer
Post the code you have tried. You have to: - open the file - give it a name - use readlines() to make a list of titles - print the first letter of each title and its length - for all the titles except the last one, shorten the length by 1 so as not to count the "\n" at the end. - close the file
Very easy way to beat this challenge by cheating (se liga nessa João kkkkk🤣🤣🤣): file = open("/usercode/files/books.txt", "r") print('H12') print('T16') print('P19') print('G18') file.close()
probably by using title(). But the only way anyone can help for sure is by posting your code, with a description of where you're stuck JFMINEBOY João Felipe post your code so we can see where the problem might be
JFMINEBOY João Felipe 👍
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
So far so good. Now that you have a list of the titles, loop through it and print the first character and length of each title.
I finished the task but I wonder why \n counts as one character instead of two ("\" and "n")
Rami Muleys line is cyclic. line is the first character. If you have 10 characters, line is the last character. Due to cyclic, line[-1] is the also last character. And this is more dynamic and it works all for numbers of character length.
How using readlines? I am trying titles = file.readlines()
But i get invalid syntax on a line that do not exists?!
JFMINEBOY João Felipe I suspect you may have copied and pasted a nonprinting character. I suggest starting over with a new code and type it in from scratch.
thank you Lam
- open the file - give it a name - use readlines() to make a list of titles - print the first letter of each title and its length - for all the titles except the last one, shorten the length by 1 so as not to count the "\n" at the end. - close the file file = open("/usercode/files/books.txt", "r") #your code goes here lines = file.readlines() for line in lines: letter = line if line[-1] == "\n": number = len(line)-1 else: number = len(line) print (str(letter) + str(number)) file.close()
\n here is a new line tag so it is counted as one
ould you please explain why line[-1] is considered as the end of the string?