Please how do I go about this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please how do I go about this code?

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 Starting with👇 file = open("/usercode/files/books.txt", "r") #your code goes here file.close()

2nd Jan 2022, 6:26 PM
Abiye Iniabere
Abiye Iniabere - avatar
2 Answers
+ 3
As you can see here once the file containing the list of books is open there are a few ways you can go about it. file is the object you will be reading from and it has been opened in read mode for you, follow it with a . and then the IO method you choose, e.g. read(a) - will read up to the number of bytes given as 'a', by default if no value is given it defaults to the value -1 meaning it will keep reading until the EOF (End Of File) marker is encountered. readline(a) - will read a single line from the document, if 'a' is given it will only read up to 'a' number of bytes, should that be less than the line contains. readlines(a) - will read lines until the byte limit of 'a' is reached, if it is not given, all lines in the file will be read. Additionally it also mentions a for loop can be used to iterate over each line in a file in turn, e.g. for line in file:. Given what needs to be done you will need to be able to look at each title individually to discover it's first character and the length of the title to create the requested output. For the remaining parts of the code, you will need to manipulate the data you receive, I would recommend looking at string methods to modify data, built-in functions for finding the length, slicing and concatenating for grabbing the parts you want, how to alter data types, and how to index a string. Links are below, good luck!
2nd Jan 2022, 11:09 PM
AnonyMouse - avatar
+ 2
To add to AnonyMouse 's answer, just note that the readlines() method does not trim the newline just before the first character in each line (except the first), so you'd have to do it manually. A possible way: # sample file open("test.txt", "w").write("1\n2\n3\n4\n5") # making a new-line-filtered version of it, after reopening it in the read mode lines = map(str.rstrip, open("test.txt", "r")) # finally printing them for x in lines: print(x) Or just: print(*open("test.txt", "r"), sep="") P.S. Remember to close the file descriptor in the end
3rd Jan 2022, 7:08 AM
Œ ㅤ
Œ ㅤ - avatar