+ 1

Has anyone been having difficulty with the Book Titles Challenge not matching the expected result?

I just worked through the Book Titles challenge, and the expected result was the same every single time, but when I read the entire book titles in the file, they didn't even come close to being something that would generate the expected result. Has anyone else had that problem?

26th Nov 2020, 7:54 AM
chuck
chuck - avatar
15 Respostas
+ 5
x = file.read(1) <-- this inside the loop is where your 1st problem is If you do a print prior to the loop of that line you'll see that you will get the actual first character of the file. The cursor is moved to the next line for each iteration of the loop so that is why your getting the letter at the beginning of the next line instead. Use i[0] instead to get the 1st letter of the current line inside the loop. You then still have 2 remaining issues. You need the len of the current line minus the '\n' character for the fist 3 lines. Then you need the total len of the last line, as it doesn't have the '\n' character.
26th Nov 2020, 6:12 PM
ChaoticDawg
ChaoticDawg - avatar
+ 5
Here is is simple and easy to read and understand solution for this task. for line in open('/usercode/files/books.txt','r'): print(f'{line[0]}{len(line.strip())}')
26th Nov 2020, 6:42 PM
Lothar
Lothar - avatar
+ 3
Remember the last line of the file doesn't have the '\n' character at the end of the line. So if you remove/subtract one, the answer will incorrect. Otherwise, you'll need to share your code so we can help further.
26th Nov 2020, 8:16 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Chibright , please do not post your question in an existing post with a total different subject. Please create a new post for your question. Thanks!
27th Nov 2020, 12:15 PM
Lothar
Lothar - avatar
+ 2
I have just tested it and everything works as usual.
26th Nov 2020, 5:13 PM
JaScript
JaScript - avatar
+ 2
Maybe, share your code so we can actually help you.
26th Nov 2020, 5:51 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
That's weird. When I run it, it says the 1st line of expected output is H12, but the 1st line of the file it's giving me in the challenge is "The Hunger Games" which results in a code of T16 (1st letter, followed by the length of the line). Maybe the expected output is starting at the 5th character...?
26th Nov 2020, 5:48 PM
chuck
chuck - avatar
+ 1
file = open("/usercode/files/books.txt", "r") #your code goes here for i in file: x = file.read(1) y = len(i) code = x + str(y) print(code) file.close()
26th Nov 2020, 5:57 PM
chuck
chuck - avatar
+ 1
Sololearn provided the 1st & last lines of the code
26th Nov 2020, 6:04 PM
chuck
chuck - avatar
+ 1
xa = file.readlines() for i in range(len(xa)-1): print(xa[i][0]+str(len(xa[i])-1)) print(xa[len(xa)-1][0]+str(len(xa[len(xa)-1]))) file.close()
26th Nov 2020, 6:13 PM
JaScript
JaScript - avatar
+ 1
Thanks for the help!
26th Nov 2020, 6:14 PM
chuck
chuck - avatar
+ 1
Okay
28th Nov 2020, 6:34 PM
Chibright
Chibright - avatar
+ 1
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
15th Dec 2020, 6:40 PM
Lam Wei Li
Lam Wei Li - avatar
- 1
Maybe my explanation wasn't clear enough. One of the expected output lines was something like H12, but the corresponding book title started with a T. The problem states that the output is supposed to start with the 1st letter of the title, and T is not H. This leads me to suspect that there is a bug in the SoloLearn software.
26th Nov 2020, 9:18 AM
chuck
chuck - avatar
- 1
Print("Enter your score") Score = Int(Input ) IfScore > 100 or score < 0; Grade = 'invalid score' ElifScore > 75; Grade = 'A' Elif > 70; Grade = 'AB' ElifScore > 65; Grade = 'B' ElifScore > 60; Grade = 'BC' ElifScore > 55; Grade = 'C' ElifScore > 50; Grade = 'CD' ElifScore > 45; Grade = 'D' ElifScore > 40; Grade = 'E' Else Grade = 'F' Help me out on where i made mistake on that🙏🙏🙏🙏🙏 Python
27th Nov 2020, 11:54 AM
Chibright
Chibright - avatar