+ 1

The fifth project in python core

I tried alot but I didn't do it so I decided to do a line for every book and it is not a good thing if I work in a library and the a hundreds of book I need to make a program take the first character of the book and the length of it Like: Input : atomic habits Rich dad poor dad Output:a13 R17 Pls help me my code is manual I need some thing automatic I will insert my code here https://code.sololearn.com/cR98HsFmU3qY/?ref=app

8th Jun 2022, 9:54 PM
ZIZO Abd alkawy
12 odpowiedzi
+ 1
ZIZO Abd alkawy , You can't do this in a generator ☺️ for i in file1: print(i[0]+str(len(i.strip())))
9th Jun 2022, 6:18 PM
Solo
Solo - avatar
+ 2
Then remove the \n with .strip() i = i.strip()
9th Jun 2022, 1:01 PM
Ajraf Miah
Ajraf Miah - avatar
+ 1
You have multiple repetitive commands, why don't you use a loop.
8th Jun 2022, 10:39 PM
Solo
Solo - avatar
+ 1
You can use a for loop to iterate over your books.txt. For each item return the first letter and the length of the string. You can start by putting your books into a list
8th Jun 2022, 11:02 PM
Ajraf Miah
Ajraf Miah - avatar
+ 1
Ajraf Miah Solo it is done.thanks for your help?
10th Jun 2022, 10:07 AM
ZIZO Abd alkawy
0
Ajraf Miah Solo It will not work because the readlines func and the for loop add a "\n" Like this : [Something\n,anotherthing\n,last one] They add a \n except the last one which mustn't be included in the length of the book If the list =[atomic habits, rich dad poor dad] The output will be: a14 not 13 and it is wrong R17 right because it is the last one in the list Pls if you get the answer insert your code and thanks for your help
9th Jun 2022, 7:31 AM
ZIZO Abd alkawy
0
Even if you don't know the strip() method, you could write a condition to search for '\n', or use a filter, there are many ways to solve it. In any case, you were given in this section of the course all the information to solve this task, which gives you the opportunity to determine how well you have studied the material covered, and whether it is worth going further.
9th Jun 2022, 1:39 PM
Solo
Solo - avatar
0
Solo Ajraf Miah This is my code and the erroe is "cannot to assign to function call"
9th Jun 2022, 5:58 PM
ZIZO Abd alkawy
0
Solo Ajraf Miah file1=file.readlines() for i.strip() in file1: print(i[0]
9th Jun 2022, 5:58 PM
ZIZO Abd alkawy
0
For i.strip() in file1: Print(i[0]+str(len(i))
9th Jun 2022, 6:00 PM
ZIZO Abd alkawy
0
Solo How can I do it without strip func? If you have time to answer me.
10th Jun 2022, 10:07 AM
ZIZO Abd alkawy