+ 3
Intermediate python working with files
soRRY ABOUT THE CAPS BUT MY DEVICE WONT TURN THENCAPS OFF IM STUCK FOR THE THREE LESSONS READING THROUGH BOOK CLUB FILLINGBUP WITH NUMBERS THEY ARE FAILING ON ALL CASES BOOK CLUB file = open("/usercode/files/books.txt", "r") for line in file: nums= list(line) x=len(nums)-1 print(line[0]+str(x)) file.close()
10 ответов
+ 8
I have made it!
line_num = 0
with open("/usercode/files/books.txt") as f:
for line in f.readlines():
line_num += 1
print("Line"+" "+str(line_num)+":"+" "+str(len(line.split()))+" "+"words")
f.close()
+ 5
with open("/usercode/files/books.txt") as f:
#your code goes here
for count, line in enumerate(f.readlines()):
print(f'Line {str(count+1)}: {str(len(line.split()))} words')
+ 3
with open("/usercode/files/books.txt") as f:
#your code goes here
words = f.readlines()
for x in (range(len(words))):
print("Line", str(x+1)+":", len(words[x].split()),"words")
+ 2
I have an answer on your other similar thread.
https://www.sololearn.com/Discuss/2699121/?ref=app
+ 2
with open("/usercode/files/books.txt") as f:
#your code goes here
amountWords = 0
lines = 0
for line in f:
lines += 1
amountWords += 1
print("Line" + " " + str(lines) + ":" + " " + str(len(line.split())) + " " + "words")
f.close()
+ 1
here is the description
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.
+ 1
A little tweak on Swapnil Kamdi's code, it is correct, but you don't have to + on spaces, spaces inside Quotation mark does count.
Example:
print("Line"+" "+str(line_num)+":"+" "+str(len(line.split()))+" "+"words")
Tweaked example:
print("Line " + str(line_num) + ": " + str(len(line.split())) + " words")
Note: Not that big of a deal, but could make the code easier to read.
Tweaked code:
line_num = 0
with open("/usercode/files/books.txt") as f:
for line in f.readlines():
line_num += 1
print("Line " + str(line_num) + ": " + str(len(line.split())) + " words")
f.close()
0
not sure how to do line 1: h12 and the other three lines
0
line_num = 0
with open("/usercode/files/books.txt") as f:
for line in f.readlines():
line_num += 1
print("Line"+" "+str(line_num)+":"+" "+str(len(line.split()))+" "+"words")
f.close()
0
My code is here
with open("/usercode/files/books.txt") as f:
#my code
text=f.readlines()
for i in range(len(text)):
print(f"Line {i+1}:",len(text[i].split()),"words")
i+=1