+ 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.
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()
+ 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...
+ 3
spaceRubyprincess check this one out
https://code.sololearn.com/cRbR39eWV6ba/?ref=app
+ 2
Revise lists, strings, indexing and it will become easier to solve.
+ 2
for line in cont:
words = line.split()
for word in words:
print(word[0],end="")
print()
+ 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()
+ 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()
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()
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()
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()
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()
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()
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()
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()
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.
- 2
I solved this problem in this way
In my computer it works but not in solo