+ 5

Intermediate Python Working with Files "Title Encoder" Project Help

I'm almost finished with the Intermediate Python course, but I'm stuck on the last project, for the Working with Files module. Here are the instructions: "You are given a file named "books.txt" with book titles, each on a separate line. To encode the book titles you need to take the first letters of each word in the title and combine them. For example, for the book title "Game of Thrones" the encoded version should be "GoT". Complete the program to read the book title from the file and output the encoded versions, each on a new line." This is what I have so far: https://code.sololearn.com/c39A0a5A8a2A As you can see, I managed to get the first letter of each word in each title, but I don't know how to combine each title's code. Instead, I just have all the first letters of all the words of all the titles in one long line, one character at a time. Any help would be very much appreciated.

6th Feb 2021, 8:08 PM
Matt Chaplin
Matt Chaplin - avatar
15 ответов
+ 1
Matt Chaplin you can change the code to following : for line in cont: words = line.split() for word in words: print(word[0],end="") print() default value of end argument is newline character , the above will cause it to print in same line .
6th Feb 2021, 9:31 PM
Abhay
Abhay - avatar
+ 5
This is a clever way using nested loops file = open("/usercode/files/books.txt", "r") books = file.readlines() file.close() print("\n".join(["".join([words[0] for words in book.split()]) for book in books]))
23rd Jul 2021, 2:24 AM
Brayan Calderon
Brayan Calderon - avatar
+ 3
Matt Chaplin this should only show you a possible way to solve it. The idea is first to build your output and after that printing it. The realisation as follows happy coding: https://code.sololearn.com/cH9n67khk3Rr/?ref=app
6th Feb 2021, 9:41 PM
JaScript
JaScript - avatar
+ 3
file = open("/usercode/files/books.txt", "r") for title in file: for word in title.split(): print(word[0], end="") print() file.close()
25th Aug 2021, 3:23 PM
TrebleClef20
TrebleClef20 - avatar
+ 2
JaScript that's a different question . But i am not sure how it would help here and if it do in any way ,sorry for my ignorance ^.
6th Feb 2021, 9:28 PM
Abhay
Abhay - avatar
+ 2
This is the answer in python. Over here they have used the split method along with files file = open("/usercode/files/books.txt", "r") cont = file.readlines() for line in cont: words = line.split() for word in words: print(word[0],end="") print() file.close()
24th Jul 2021, 3:00 PM
Aayushi Jha
Aayushi Jha - avatar
6th Feb 2021, 8:20 PM
JaScript
JaScript - avatar
+ 1
file = open("/usercode/files/books.txt", "r") txt = "" cont = file.readlines() for line in cont: words = line.split() for word in words: txt += word[0] print(txt) txt = ""
15th Aug 2021, 6:47 PM
Vraj Soni
Vraj Soni - avatar
+ 1
I solved this way file = open("/usercode/files/books.txt", "r") cont = file.readlines() for line in cont: words = line.split() for word in words: print(word[0],end="") print() file.close()
1st Oct 2021, 4:16 PM
Hariom Kumar
Hariom Kumar - avatar
0
JaScript, I'm sorry, but the code you linked only helps me about as far as I've already gotten. I'm not sure how I would convert the code you've used to give me the first letter of each word in each title, rather than the first letter of the title and the length. Honestly, I don't think I would have a problem doing it that way. It's the words/lines separation that's confusing me. Thank you though.
6th Feb 2021, 8:34 PM
Matt Chaplin
Matt Chaplin - avatar
0
Abhay, thank you!!!
7th Feb 2021, 10:49 PM
Matt Chaplin
Matt Chaplin - avatar
0
This should help file = open("/usercode/files/books.txt", "r") letters=[] for line in file.readlines(): words=line.split() for w in words: letters.append(w[0]) print("".join(letters),end="") letters=[] file.close()
12th Jul 2021, 8:57 PM
Laxman Bist
Laxman Bist - avatar
0
file = open("/usercode/files/books.txt", "r") book_L=file.readlines() for c in range(len(book_L)): print(''.joint([r[0] for r in book_L[c].split()])) file.close()
13th Feb 2022, 12:57 AM
Alcino Castelo
0
file = open("/usercode/files/books.txt", "r") #your code goes here books = file.readlines() print(type(books)) for book in books: name = book s = name.split() t = [] for i in s: t.append(i[0]) print("".join(t)) file.close() please whats wrong with this code, the output is correct but sololearn says its wrong
1st Aug 2022, 12:39 AM
Bamisile Tolulope
Bamisile Tolulope - avatar
0
cont = file.readlines() for line in cont: words = line.split() for word in words: print(word[0], end = "") print() This didn't pass the test. Any suggestions
26th Dec 2022, 9:57 PM
Kishore Kumar