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?
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?
+ 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())))
+ 1
Crimind.Ke you may also want to check out the modes that are used while opening a file . "a" appends to current file . It doesn't allows reading from it .
https://www.google.com/url?sa=t&source=web&rct=j&url=https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/file-operation&ved=2ahUKEwiC8sDct9jyAhX4xzgGHQZDBV0QFnoECAMQBQ&usg=AOvVaw3WvDDO7I7eyNDnMKYjTGGf
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."