+ 3

help please i dont really know what to do

Im in the last project of intermediate python and i dont know how to solve it: 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. You can use the .split() method to get a list of words in a string.

26th Feb 2021, 7:20 PM
Yami Francø
Yami Francø - avatar
17 Respostas
+ 27
Show your attempt, please. And check this one. file = open("/usercode/files/books.txt", "r") for line in file.readlines(): words = line.split() encode ='' for word in words : encode = encode + word[0] print (encode) file.close()
4th Mar 2021, 8:04 AM
Sadokatkhon Abdukhalilova
Sadokatkhon Abdukhalilova - avatar
4th Mar 2021, 8:06 AM
Sadokatkhon Abdukhalilova
Sadokatkhon Abdukhalilova - avatar
+ 4
Steps already given : 1) .Read the file "books.txt". You will get lines of text with book titles. read about function : open("filename") 2) Use split() on lines of text. You get list of words read about split() method 3) print 1st letter of each word. list slicing word[0] for first charecter share your try...
26th Feb 2021, 7:34 PM
Jayakrishna 🇮🇳
26th Feb 2021, 8:15 PM
Kingsley Aham
Kingsley Aham - avatar
+ 2
Revise lists, strings, indexing and it will become easier to solve.
26th Feb 2021, 7:55 PM
Sonic
Sonic - avatar
+ 2
for line in cont: words = line.split() for word in words: print(word[0],end="") print()
2nd Jun 2021, 6:02 AM
swetha R
+ 2
Thank you very much i Identified the mistake using this code Show your attempt, please. And check this one. file = open("/usercode/files/books.txt", "r") for line in file.readlines(): words = line.split() encode ='' for word in words : encode = encode + word[0] print (encode) file.close()
22nd Dec 2021, 6:42 AM
Nethula Jayalath
+ 1
The shortest and simplest way took only 3 lines: file = open("/usercode/files/books.txt", "r") #your code goes here book_list = file.readlines() for b in range(len(book_list)): print(''.join([i[0] for i in book_list[b].split()])) file.close()
23rd Oct 2021, 11:02 AM
Voitsekh Ken
Voitsekh Ken - avatar
0
I did it this way: file = open("/usercode/files/books.txt", "r") #tu código va aquí lines = file.readlines() for i in range(len(lines)): if i != (len(lines) - 1): long = len(lines[i]) - 1 else: long = len(lines[i]) #print(i) print(str(lines[i][:1]) + str(long)) # print(long) file.close()
26th Feb 2021, 8:06 PM
tom
tom - avatar
0
file = open("/usercode/files/books.txt", "r") cont = file.readlines() cont1 = [] for i in range(len(cont)): name = [] cont1 = list(cont[i]) for j in range(len(cont1)): if cont1[j].isupper(): name.append(cont1[j]) elif cont1[j] == " " and cont1[j+1].islower(): name.append(cont1[j+1]) while ' ' in name: name.remove(' ') name1 = "" for l in name: name1 += l print(f"\"{name1}\"") file.close()
3rd Mar 2021, 4:49 PM
Ozodbek
Ozodbek - avatar
0
file = open("/usercode/files/books.txt", "r") for lines in file.readlines(): words=lines.split() encode='' for word in words: encode+=word[0] print(encode) file.close()
3rd Apr 2021, 4:16 PM
rajath br
rajath br - avatar
0
# another way to solve problem with open("/usercode/files/books.txt", "r") as f: for lines in f.readlines(): words = lines.split() # print(words) encode="" for word in words: encode = encode + word [0] print(encode) file.close()
2nd Jul 2021, 10:08 AM
Hossein Eshraghi
Hossein Eshraghi - avatar
0
file = open("/usercode/files/books.txt", "r") for lines in file.readlines(): words=lines.split() encode='' for word in words: encode+=word[0] print(encode) file.close()
16th Dec 2021, 6:07 AM
Marvin Manaog
Marvin Manaog - avatar
0
file = open("/usercode/files/books.txt", "r") for line in file.readlines(): words = line.split() encode ='' for word in words : encode = encode + word[0] print (encode) file.close()
17th Dec 2021, 10:23 AM
Ismoilov Abdug'ofur
Ismoilov Abdug'ofur - avatar
0
Show your attempt, please. And check this one. file = open("/usercode/files/books.txt", "r") for line in file.readlines(): words = line.split() encode ='' for word in words : encode = encode + word[0] print (encode) file.close()
31st Mar 2022, 11:27 PM
Abdelkhalek Nael Ahmad Allan
Abdelkhalek Nael Ahmad Allan - avatar
0
cont = file.readlines() for line in cont: words = line.split() for word in words: print(word[0], end = "") print() file.close() This didn't clear the test. Any suggestions.
26th Dec 2022, 10:22 PM
Kishore Kumar
- 2
I solved this problem in this way In my computer it works but not in solo
3rd Mar 2021, 4:51 PM
Ozodbek
Ozodbek - avatar