Help me with this... | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

Help me with this...

file = open("/usercode/files/books.txt", "r") #your code goes here book_titles = file.readlines() total_books = len(book_titles) book_index = 0 for i in range(total_books): book_name = book_titles[i] first_letter = book_name[0] if i < total_books: name_length = len(book_name)-1 book_code = first_letter + str(name_length) print(book_code ) file.close()

15th Aug 2021, 11:44 AM
Ahnaf Sadaf
Ahnaf Sadaf - avatar
8 Réponses
+ 3
Because the for loop infex „i“ beginning from 0. So the last item will have the index len(array)-1.
16th Aug 2021, 7:11 AM
JaScript
JaScript - avatar
+ 3
You have to pay attention to which line should be where so that it can be influenced by "if" or not. file = open("/usercode/files/books.txt","r") #your code goes here book_titles = file.readlines() total_books = len(book_titles) book_index = 0 for i in range(total_books): book_name = book_titles[i] first_letter = book_name[0] if i < total_books: name_length = len(book_name)-1 book_code = first_letter + str(name_length) print(book_code ) file.close()
15th Aug 2021, 11:51 AM
JaScript
JaScript - avatar
+ 3
Please save on SL Playground and link your attempt here.
15th Aug 2021, 11:59 AM
JaScript
JaScript - avatar
+ 2
Sorry , but you just fixed the indentation. I made the indentation correct in the actual code. But still don't work
15th Aug 2021, 11:55 AM
Ahnaf Sadaf
Ahnaf Sadaf - avatar
15th Aug 2021, 12:00 PM
Ahnaf Sadaf
Ahnaf Sadaf - avatar
+ 1
Thanks man, it really worked
15th Aug 2021, 12:06 PM
Ahnaf Sadaf
Ahnaf Sadaf - avatar
+ 1
Can you please explain me why I need to write " i < (total_books-1)"... I'm not understanding the purpose of -1
16th Aug 2021, 5:54 AM
Ahnaf Sadaf
Ahnaf Sadaf - avatar
+ 1
Ohh ..got it.. Thanks man
16th Aug 2021, 12:20 PM
Ahnaf Sadaf
Ahnaf Sadaf - avatar