Has anyone been having difficulty with the Book Titles Challenge not matching the expected result? | SoloLearn: Learn to code for FREE!

+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?

11/26/2020 7:54:05 AM

chuck

15 Answers

New Answer

+4

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.

+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())}')

+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.

+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!

+2

I have just tested it and everything works as usual.

+2

Maybe, share your code so we can actually help you.

+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...?

+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()

+1

Sololearn provided the 1st & last lines of the code

+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()

+1

Thanks for the help!

+1

+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

-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.

-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