Please can someone help me to fix this code? I have to buy the pro version to see why it isn't working. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please can someone help me to fix this code? I have to buy the pro version to see why it isn't working.

file = open("/usercode/files/books.txt", "r") char_list =[] for line in file.readlines(): list = line.split(" ") first_char = [char[0] for char in list] char_list = char_list.append(first_char) print("".join(char_list)) file.close() The goal is to read each line of a text file, and print out the first letter of each string as an abbreviation. They used the example of Game of Thrones being reduced to GoT. It recommends you use the split() method to do so. This is the last module from the intermediate python course. Thank you so much for reading!!

31st Jan 2021, 9:41 AM
Max Hill
Max Hill - avatar
6 Answers
+ 1
its the same code but waaay more simple, here: https://code.sololearn.com/cDlsyfSTBo1m/?ref=app
31st Jan 2021, 10:02 AM
Slick
Slick - avatar
+ 1
You don't have to buy pro, they can't see the answers either. It looks like the "Book Titles" problem. https://www.sololearn.com/Discuss/2589736/?ref=app
31st Jan 2021, 9:49 AM
Slick
Slick - avatar
+ 1
Ohhhhhhh that makes a lot more sense now! Thank you so much!! 😁
31st Jan 2021, 10:03 AM
Max Hill
Max Hill - avatar
+ 1
file = open("/usercode/files/books.txt", "r") def book_condense(book_title): separated = book_title.split() first_letters = [title[0] for title in separated] return ''.join(first_letters) for line in file.readlines(): print(book_condense(line)) file.close() It worked!!
31st Jan 2021, 10:12 AM
Max Hill
Max Hill - avatar
0
Oh no I've completed that module (it drove me insane for days). This module is from one of the smaller courses sololearn offer called Intermediate Python. It's the last module of the course and it says to read each line of the file and combine the first letters of each word together. The length of the string isn't accounted for in this module, it's just purely a combination of the first letters together (like Harry Potter and the Goblet of Fire would produce HPatGoF )
31st Jan 2021, 9:56 AM
Max Hill
Max Hill - avatar
0
oh, then its basically the same code! but instead of worrying about the length of a line, you just split each line and sandwich together the first letter of each word (Using indexing)
31st Jan 2021, 9:58 AM
Slick
Slick - avatar