0

someone please help me check this out

file = open("/usercode/files/books.txt", "a") #your code goes here BookTittle = input() abv = BookTittle.split() for words in abv: x = print(words[0], end='') file.write(x) file.close() what's wrong with this?

30th Aug 2021, 8:55 AM
Crimind.Ke
Crimind.Ke - avatar
4 Réponses
+ 3
First problem is there is an indentation issue on the line which starts with abv. After that, I am not sure what you are trying to achieve. Can you show an input example and an output example. It looks like you are trying to capture the first letter of each word & join them together before you write this collection to a file?
30th Aug 2021, 9:04 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
What is this ? No mention about where is this question from and no language name that indicates what code it is . Can you use those tags for a language name that is relevant to the code you have in description ? I have done this question so i can help you but others don't have a clue about what you want them to check out . Why are you taking input from user ? The file that you opened returned a pointer (file) to the starting of file . Now to read the contents you can either use read() , readlines() or simply loop over it . For example, for i in file: #i is assigned each line in file now . Also each line has a newline character ("\n") appended to it except the last line. You can use rstrip() to remove any whitespace(including newline) from end of string. So the code will be as follow, for i in file: print(i[0] + str(len(i.rstrip())))
30th Aug 2021, 9:05 AM
Abhay
Abhay - avatar
30th Aug 2021, 9:32 AM
Abhay
Abhay - avatar
0
This is what I'm trying in Python "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. Complete the program to read the book title from the file and output the encoded versions, each on a new line."
30th Aug 2021, 10:31 AM
Crimind.Ke
Crimind.Ke - avatar