0
Does anyone know how to solve this?
You have been asked to make a special book categorization program, which assigns each book a special code based on its title. The code is equal to the first letter of the book, followed by the number of characters in the title. For example, for the book "Harry Potter", the code would be: H12, as it contains 12 characters (including the space). You are provided a books.txt file, which includes the book titles, each one written on a separate line. Read the title one by one and output the code for each book on a separate line. For example, if the books.txt file contains: Some book Another book Your program should output: S9 A12 file = open("/usercode/files/books.txt", "r") #your code goes here file.close()
6 Answers
+ 4
Please show your attempt first so we may help. We cannot just give you the solution as we believe it would prevent you from learning. Review your lessons and try to attempt it. Thanks for understanding!
+ 2
It is because there is an escape sequence or newline "\n" as the last character of every string, or every line. You can use .strip() string method to remove those.
string.strip()
The rest is up to you. If there are still bugs/errors, feel free show us your attempt. Thanks!
+ 1
finally i did it.
file = open("/usercode/files/books.txt", "r")
a = 0
#your code goes here
for i in file:
if str(a) <= str(len(i)):
print(i[0]+str(len(i)-1))
else:
print(i[0]+str(len(i)))
a+=1
file.close()
+ 1
Aurora That's another solution cause the last line does not have "\n". And by using if-else statement. Nice!
Glad you solved it, Great work!đ
0
file = open("/usercode/files/books.txt", "r")
#your code goes here
for i in file:
print(i[0]+str(len(i)))
file.close()
This is my answer. But it is wrong.
0
The answer should be
H12
T16
P19
G18
But my answer is
H13
T16
P19
G18