Can anyone pls help me with the book titles module project | Sololearn: Learn to code for FREE!

+1

Can anyone pls help me with the book titles module project

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

1/18/2021 1:38:30 AM

yee ya

8 Answers

New Answer

+2

yee ya ( 1 ) The len will not cause an error but the + will, because the len(i) will return an integer and we cannot concatenate integer and string unless if we convert that integer to string. ---> str(len(i)) ( 2 ) The variable "i" in the "for loop" represents each line of the file content therefore we dont need to read the file again by using "file.read(1)", we can just use the variable "i" instead. To get the first letter of each line: ---> string[0] or i [0] Additional: string.strip() as I said earlier will remove the spaces and "\n" at the end of the string because we only need to count the length of the character of the string exluding the escape character. https://code.sololearn.com/cQtOmn0fxpG1/?ref=app

+3

yee ya Well.. same here. All the best! and Good luck to both of us!

+2

Please show us your attempt so far so we may help. Thanks Here are some hints: len ---> length of string You can use file.readlines() to have a list with each line of the file as its each element. Or you can just iterate the file by using for loop. string.strip() removes the "\n" at the end of the string.

+2

yee ya Actually yes you can do some math, if-else condition to be specific. If "i" variable has "\n" at the end then, "len(i) - 1" Else if the "i" variable is the last line of the file i.e. no "\n", then just "len(i)" This is the common solution I see and this is working properly but just longer. https://code.sololearn.com/cr66jfio7h78/?ref=app

+1

I tired doing: file = open("/usercode/files/books.txt", "r") #your code goes here for i in file: print(file.read(1) + len(i)) file.close() However the len(i) causes an error And the file.read(1) does not include the first letter of the item in the list

+1

Thank u so much!! I've been looking for that string.strip() I thought i have to do some math thing to get the right output numbers

+1

I guess i still have a long way to go

+1

《 Nicko12 》 to me ur like a pro i could barely even do this module projects But i wanna do better to build something in the future Good luck to you too!