I’ve been trying to solve the below simple exercise but no code I’ve tried worked so far, I’m using python 3 spyder
Write a Python script that will read a text file whose lines contain numbers separated by white-space; as output, the program will produce for each line of the original text file another line that will contain two numbers: the first will be the total number of entries in the line, and the second will be the sum of the numbers in the line.
Well! I kept erasing cause nothing was producing the required result, I tried may be converting the txt file to a list and that didn’t work, I tried using a for loop and it didn’t work either. I’m a complete and total beginner in python and coding altogether and so this is super confusing!
f= open("test1.txt", "r")
print(f.read())
with open('test1.txt') as fp:
list_1 = [list(map(int, line.strip().split(' '))) for line in fp]
total= 0
for line in fp:
total += int(line)
print(total)
I have no idea what triple backticks even mean!!! LORD!!! that is how new and clueless I’m lol. Anyways I’ve copied my code exactly how I entered it into python, I hope this helps. Sorry for the inconvenience.
f= open("test1.txt", "r")
print(f.read())
with open('test1.txt') as fp:
list_1 = [list(map(int, line.strip().split(' '))) for line in fp]
total= 0
for line in fp:
total += int(line)
print(total)
f= open(“test1.txt”, “r”)
print(f.read())
with open(‘test1.txt’) as fp:
list_1 = [list(map(int, line.strip().split(’ '))) for line in fp]
total= 0
for line in fp:
total += int(line)
print(total)
f= open("test1.txt", "r")
print(f.read())
with open('test1.txt') as fp:
>>>>list_1 = [list(map(int, line.strip().split(' '))) for line in fp]
>>>>total= 0
>>>>for line in fp:
>>>>>>>>>total += int(line)
print(total)
OK so this is the best I could do to present an idea of the indentation.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
I’ve been trying to solve the below simple exercise but no code I’ve tried worked so far, I’m using python 3 spyder
Write a Python script that will read a text file whose lines contain numbers separated by white-space; as output, the program will produce for each line of the original text file another line that will contain two numbers: the first will be the total number of entries in the line, and the second will be the sum of the numbers in the line.
file=open("a.txt",'r')
lines=file.readlines()
for line in lines:
arr=line.split(" ")
total=len(arr)
sumNum=0
for num in arr:
sumNum+=int(num)
print(str(total)+" "+str(sumNum))
Hi @FatmaNagori
Thank you for helping @zsam and to take the time to come up with a solution! Do you think you could maybe make your answer even better by explaining how you came up with your solution? Maybe provide some hints and a spoiler alert for the full solution? There is a useful spoiler alert feature built into the forum editor: