- 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())
25 Respuestas
+ 2
Zach Z print(f"Line {i+1}: {len(j.split())} words")
+ 2
Abhay just following up here. Thanks! Can you provide the full code?
+ 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 .
+ 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.
+ 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.
+ 1
Ok thanks Abhay , im a little lost with all of the back and forth. Can you put the complete code please?
+ 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!
+ 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.
+ 1
Bingo. Thanks Abhay !
+ 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")
+ 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
+ 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")
0
Thanks Abhay but still not sure whats next?
0
Really appreciate it Abhay . Ya sorry, still not sure. Can you share more?? Thanks!
0
JaScript can you please help here? Thanks!
0
Zach Z really sorry that i couldn't help but you could have just asked a new question and delete this one.
0
Zach Z that was complete code and it worked fine for me
0
No problem Abhay. The question and start to the code are above.
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())" .
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.