Python book title program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Python book title program

So Umm basically I am stuck on a program. Basically I am near at the end but Still couldn't finish it. It tells us to make a program with diferrent book title and make a code and write it to a file and output it in seperate like. I managed to get the title code but how can I take different input ??? Description and my code are below . 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 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} s= "History book" y=str(len(s)) file = open("booktitle.txt", "w") file.write(s[0]+y) file.close() file = open("booktitle.txt", "r") x = file.read() file.close() print(x)

4th Nov 2020, 11:35 AM
Umar Haque
Umar Haque - avatar
9 Answers
+ 2
Don't write to a file. That's not what the challenge tells you to do. Read the file and for each line print the code
4th Nov 2020, 11:44 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
The first step will be read carefully the description. The second run the code in Playground and test with a few input‘s data and prints in different code positions, how that works and what happend. Then you can maybe find out and correct this problem yourself. If not of course you can ask here for help. The input of this task is with „\n“ prepared. So it will be needed something to do with the code for getting the requested output. If you want you can see a solution here: https://code.sololearn.com/cPeBWF4tkD8w/?ref=app
4th Nov 2020, 11:51 AM
JaScript
JaScript - avatar
+ 2
A simpler way with 5 lines of code. A simple explanation, loop through all lines, and if a line has trailing/ending "\n" remove it from the count of the length. https://code.sololearn.com/cdaPdMWWeN3W/?ref=app
29th Dec 2020, 6:27 PM
Lam Wei Li
Lam Wei Li - avatar
+ 1
The file already have book titles.. So just read file lines and print needed output.. First charecter of book titles. And length of book title..
4th Nov 2020, 11:42 AM
Jayakrishna 🇮🇳
+ 1
For one second can someone try to explain the solution to this problem WITHOUT using operators or methods not described in the modules already seen up until the Book title problem?
13th Mar 2021, 7:03 PM
Samuel Landing
0
#i feel its the right answer file = open("/usercode/files/books.txt", "r") #your code goes here b=file.readlines() for i in b: i=i.rstrip("\n") print(i[0] + str(len(i))) file.close()
8th Nov 2020, 4:16 PM
Rupan Dutta
Rupan Dutta - avatar
0
Samuel Landing My solution, unlike many others, did not use strip() function which is not described in modules. str() is in Functions & Modules 32.1 len() is in Exceptions & Files 48.1
14th Mar 2021, 3:14 AM
Lam Wei Li
Lam Wei Li - avatar
- 1
file = open("/usercode/files/books.txt", "r") #your code goes here b=file.readlines() for i in b: if len(i)==18: c=i[0]+str(len(i)) print(str(c)) else: p=len(i)-1 x=i[0]+str(p) print(str(x)) file.close()
8th Nov 2020, 3:52 PM
Rupan Dutta
Rupan Dutta - avatar
- 1
file = open("/usercode/files/books.txt", "r") #your code goes here books=file.readlines() for i in books: if i==books[-1]: value=len(i) else: value=len(i)-1 print(i[0]+str(value)) file.close()
25th Apr 2021, 10:29 AM
A Owadud Bhuiyan
A Owadud Bhuiyan - avatar