Intermediate Python Working with Files "Title Encoder" Project Help | Sololearn: Learn to code for FREE!

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

2/6/2021 8:08:03 PM

Matt Chaplin

14 Answers

New Answer

0

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 .

+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

+3

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]))

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

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

+2

file = open("/usercode/files/books.txt", "r") for title in file: for word in title.split(): print(word[0], end="") print() file.close()

+1

This will help you: https://code.sololearn.com/cPeBWF4tkD8w/?ref=app

+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 = ""

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

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.

0

Abhay, thank you!!!

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

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

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