- 3

Intermediate Python - 37.2 Practice help - “Book Club”

Please help! You are given a books.txt file, which includes book titles, each on a separate line. Create a program to output how many words each title contains, in the following format: Line 1: 3 words Line 2: 5 words ... Make sure to match the above mentioned format in the output. No idea what to do next, please help, thanks! Code: with open("/usercode/files/books.txt") as f: print(f.read())

30th Jul 2021, 5:26 PM
Zach Z
24 Antworten
+ 2
Zach Z print(f"Line {i+1}: {len(j.split())} words")
7th Sep 2021, 2:30 PM
Abhay
Abhay - avatar
+ 2
Abhay just following up here. Thanks! Can you provide the full code?
17th Aug 2021, 8:49 PM
Zach Z
+ 1
for f in f.readlines(): #do something with each line. remember each line ends with a newline character except the last line. And f.readlines() is an array .
30th Jul 2021, 5:32 PM
Abhay
Abhay - avatar
+ 1
Zach Z you can strip out the newline character from each line except last one and count the len of line and do whatever else program asked for . Btw you can do the following as well instead of f.readlines(): for line in f : ______________________________ And if you still can't understand then i will write the code with comment on what each line is doing.
9th Aug 2021, 8:56 AM
Abhay
Abhay - avatar
+ 1
with open("/usercode/files/books.txt") as f: for i in f: print(i[0]+str(len(i.rstrip()))) #rstrip will remove any whitespace (such as newline character) from the end of string.
28th Aug 2021, 10:59 PM
Abhay
Abhay - avatar
+ 1
Ok thanks Abhay , im a little lost with all of the back and forth. Can you put the complete code please?
29th Aug 2021, 1:44 PM
Zach Z
+ 1
with open("/usercode/files/books.txt") as f: for i in f.readlines(): print(i[0]+str(len(i.rstrip()))) Abhay this didnt work. Can you help? Thanks!
4th Sep 2021, 3:45 PM
Zach Z
+ 1
Zach Z the code you gave me works fine as well. Edit : Sorry looks like i have been helping you with a different question . Really sorry , i thought it was book titles from code coach . But now i see that it is 37.2 project from intermediate python. I don't know the original question so can't help with that unless you provide me details on what it is really asking for.
4th Sep 2021, 3:51 PM
Abhay
Abhay - avatar
+ 1
Bingo. Thanks Abhay !
8th Sep 2021, 7:06 PM
Zach Z
+ 1
Hi, for me this worked: with open("/usercode/files/books.txt") as f: #your code goes here lines = f.readlines() x=0 for i in lines: x += 1 words = str(len(i.split())) print("Line " + str(x) + ": " + words + " words")
18th Jan 2022, 12:08 PM
Martin Valdivia
+ 1
with open("/usercode/files/books.txt") as f: j=1 for line in f: print(f"Line {j}: {len(line.split())} words") j+=1
16th Dec 2022, 4:20 PM
Jordan-Kény Dansi
Jordan-Kény Dansi - avatar
+ 1
with open("/usercode/files/books.txt") as f: #lines as list listofnames = f.readlines() #iterate through list and printing for i in range(1, len(listofnames)+1): x = len(list(listofnames[i-1].split())) print("Line " + str(i) + ": " + str(x) + " words")
19th Oct 2023, 5:13 PM
Mohamed Ibrahim
Mohamed Ibrahim - avatar
0
Thanks Abhay but still not sure whats next?
9th Aug 2021, 2:52 AM
Zach Z
0
Really appreciate it Abhay . Ya sorry, still not sure. Can you share more?? Thanks!
9th Aug 2021, 9:07 PM
Zach Z
0
JaScript can you please help here? Thanks!
27th Aug 2021, 4:48 PM
Zach Z
0
Zach Z really sorry that i couldn't help but you could have just asked a new question and delete this one.
28th Aug 2021, 10:54 PM
Abhay
Abhay - avatar
0
Zach Z that was complete code and it worked fine for me
4th Sep 2021, 3:48 PM
Abhay
Abhay - avatar
0
No problem Abhay. The question and start to the code are above.
5th Sep 2021, 8:50 PM
Zach Z
0
Zach Z with open("/usercode/files/books.txt") as f: for i,j in enumerate(f): print(f"Line {i+1}:{len(j)} words") The above code is including newline character as well . If it doesn't works then replace "len(j)" with "len(j.rstrip())" .
5th Sep 2021, 9:04 PM
Abhay
Abhay - avatar
0
Hey Abhay neither is working. The second comes out with double digit numbers when the solution is single digit? Coming out to 12,16,19,18 words vs the answer of 2,3,3,4 words.
6th Sep 2021, 4:50 PM
Zach Z