Python Homework Help! Simple Code Needed

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.

Example:

Input file (test1.txt) contents:

1 3 5 12 0

2 3

1

-1 1

Expected Output:

Enter text file name: test1.txt

Output:

5 21

2 5

1 1

2 0

What have you tried so far?

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!

Uh! I also tried this:

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)

not working as well.

I’m having trouble reading your code. Can you put triple backticks on the line before and after?

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)

Put 3 of this character before and after the code in your post. Otherwise I can’t see the spaces.
```

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 did! But I’m not seeing an improvement! Can you?

Do you have indentation in your python script?

Yes! But the thing is the above code isn’t even producing anything near to what is asked of me in the question above exercise.

I can’t really help if I can’t see the indentation.

You have a lot of pieces that will help you get there, but order and indentation matters.

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 think after I pressed reply the code made no sense. Anways thank you so much for your help!

You’ve got the right basic idea.

You want to break each line, one at a time, into an array of values and then sum that array.

I’m suspicious of your line creating list_1. I’m also not seeing you sum the contents of that array.

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.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

why are you creating two topics for same question?
you can do this solution by using following steps :

  1. first open the file in read mode.
  2. readlines all lines of opened file using file.readlines() function .
  3. readlines() function returned a list of all lines so to get one line at a time use for loop.
  4. inside for loop split line at " " using line.split(" ").
  5. get the length of list returned by split() using len() function.and this is result you want i.e. total number in one line.
  6. create a variable with value 0.
  7. use for loop on list retuned by line.split(" ")
  8. now add all numbers returned by for loop using variable created before.

you can find all the above explaination as code on the bellow link .to see the code just click on the blur text.
http://forum.freecodecamp.org/t/help-simple-python-code-needed/351725/2

1 Like

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.

Example:

Input file (test1.txt) contents:

1 3 5 12 0

2 3

1

-1 1

Expected Output:

Enter text file name: test1.txt

Output:

5 21

2 5

1 1

2 0

you can do this using following code :

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))

2 Likes

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:

image

Anyways, thanks for your contribution!