0
Title Encoder
file = open("/usercode/files/books.txt", "r") books = file.readlines() print("\n".join(["".join([words[0] for words in book.split()]) for book in books])) file.close() Pls who can help me write an expanded form of what is inside the print function?
10 Antworten
+ 2
Oluwadurotimi Efunshile ,
we are missing the task description, and also the name of the python tutorial and exercise number
+ 2
Oluwadurotimi Efunshile ,
not sure if this is what you wanted to see. i have put comments to the code.
file = open("books.txt", "r")
books = file.readlines()
for book in books: # outer loop to get each line of the txt file
tmp = [] # buffer list that is used to collect the first letter of each word
for word in book.split(): # inner loop to split each line to individual words
tmp.append(word[0]) # take each individual word, get the first letter and store it in list
print("".join(tmp)) # print the collected letters from each line
file.close()
0
What do you mean by expanded form?
0
Help me to represent each statement with letters one by one
Something like:
for book in books:
....
...
...
print("\n".join...)
file.close()
0
Instead of everything on one line inside the print statement
0
#it can be expanded like the following:
file = open("/usercode/files/books.txt", "r")
books = file.readlines()
for book in books:
for words in book.split():
print(words[0],end="")
print()
file.close()
0
It's the expanded form using the join function that I actually want to get
0
Sousou it's actually the expanded form, using the join function that I want to get
0
Lothar thanks so much