(Solved)Hallo, I'm stuck with title encoder. How to get initial of book's title, example Harry Potter is 'HP'. Give me advice | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

(Solved)Hallo, I'm stuck with title encoder. How to get initial of book's title, example Harry Potter is 'HP'. Give me advice

I don't ask the code, but where my mistake is, thank you. file = open("/usercode/files/books.txt", "r") #your code goes here for i in file : for j in i.split(): a.append(j[0]) print(a) file.close()

15th Feb 2021, 5:19 AM
Ratna13
Ratna13 - avatar
6 Answers
+ 1
You don't need the nested loop nor do you need to split() the line from the file. Loop over each line of the file and output only the first character of the line concatenated to the length of the line (not including trailing whitespace, '\n') You can get the first character using the index 0 and the length by passing the str.strip() to the len function. Example; line = "Harry Potter and the Sorcerer's Stone\n" first_letter = line[0] # 'H' line_length = len(line.strip()) # 37
15th Feb 2021, 6:48 AM
ChaoticDawg
ChaoticDawg - avatar
0
No, not getting initial then the Len but the first char from each word
15th Feb 2021, 7:10 AM
Ratna13
Ratna13 - avatar
0
My code works for 1 line but, more than one not
15th Feb 2021, 7:11 AM
Ratna13
Ratna13 - avatar
0
Is this for the; 52 end of module project 'Book Titles' in the core python course?
15th Feb 2021, 7:21 AM
ChaoticDawg
ChaoticDawg - avatar
0
No, intermediate Python
15th Feb 2021, 10:09 AM
Ratna13
Ratna13 - avatar
0
Ahhh, ok, sorry thought it was a different module project. Well looks like you got it figured out anyhow.
15th Feb 2021, 10:32 AM
ChaoticDawg
ChaoticDawg - avatar