+ 2

Problems with count() in lists/files

Why does this not work and can you explain a solution please file = open("/usercode/files/books.txt", "r") #your code goes h list = file.readlines() amount = list.count('\n') print(amount)

3rd Jan 2022, 4:46 PM
Darkpidgeon 14
5 Answers
+ 6
Darkpidgeon 14 , Shahrull Fytri , (>>> read the task description carefully !!!) this is how it can be done: file = open("/usercode/files/books.txt", "r") lst = file.readlines() # do not use *list* as variable name #amount = list.count('\n') #print(amount) for book in lst: # take the first letter from *book* # count the length of *book*, but do not count *\n*, which is at the end of each line # put both together and output it. be aware of different data types
3rd Jan 2022, 6:11 PM
Lothar
Lothar - avatar
+ 5
the task description is: 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
3rd Jan 2022, 5:56 PM
Lothar
Lothar - avatar
+ 1
First thing is, don't use list as variable name because in python, there is list() constructor. Ok, I assume that your list variable is holding a list of book title looking to what it is reading from. So, in that list, there is no "\n". If you want to count how many item inside that list, use len() instead. Indeed there will be thing like: "Book 1\n" "Book 2\n" but there is no "\n". What count("\n") does is counting how many "\n" inside that list. "Book 1\n" is not consider as "\n". ``` amount = len(list) ``` Function len() is a built-in function that is used to count the number of item inside given iterator such as list.
3rd Jan 2022, 5:56 PM
Shahrull Fytri
Shahrull Fytri - avatar
0
Simba python 51 book title
3rd Jan 2022, 5:27 PM
Darkpidgeon 14
0
Based on task description sent by Lothar, what you need to do is, you need to loop through every element inside the list and get the number of character inside that item. You can use len() to get the number of character. For example, if you have a book with title "Harry Potter", just passing that title into len() function, it will give you 13. But, you need to discard the \n from the book title by using .strip("\n") method. So overall, you code will look like this: ``` book_code = [] book_list = file.readlines() for book in book_list: char_len = len(book.strip("\n")) code = f"{book[0]}{char_len}" book_code.append(code) ``` In the code above, I create a temp list that will hold the book code. the I loop through all item from book_list. In the loop, I get the number of character of that book. Then I get the book code using f-string. I got the initial letter by accessing the first item inside the book title string (book[0]) and combine it with char_len that I got previously. After that I append that code into code_list. Btw, if you don't want to use f-string, you can just combine it using + such as: ``` code = book[0] + str(char_len) ```
3rd Jan 2022, 6:09 PM
Shahrull Fytri
Shahrull Fytri - avatar