-3

I'm quite new to this so do excuse any newbie errors.

So the goal of my code is to use the 3 lines of data in a text file ("Numbers.txt") to carry out some calculations line by line. I then want to write the results of these calculations into a new text file ("Newnumbers.txt").

The "Numbers.txt" file contains the following 3 lines of data:

1, 2, 3, 4, 5, 6, 7
2, 4, 6, 8, 10, 12, 14
1, 3, 5, 7, 9, 11, 13

Whenever I run this code, only the last line seems to be read. The "calcresults" prints as [2, 4, 14, 2, 4, 14, 2, 4, 14].

Where am I going wrong? I'd very much appreciate any help :)

Here's my script:

numbers = open("Numbers.txt", "r")

for line in numbers:
    (m, a, e, i, o, O, E) = line.split(",")
    print(line)
    m = int(m)
    a = int(a)
    e = int(e)
    i = int(i)
    o = int(o)
    O = int(O)
    E = int(E)

numbers.close()

x = 0
y = 0
z = 0

calcresults = []

numbers = open("Numbers.txt", "r")

for line in numbers:
    x = m*2
    y = m+a
    z = m+E
    calcresults.append(x)
    calcresults.append(y)
    calcresults.append(z)

print(calcresults)

calcresults = str(calcresults)

newnumbers = open("Newnumbers.txt", "w")
newnumbers.write(calcresults)

numbers.close()
newnumbers.close()`
Emma H
  • 1
  • 2
  • Can you edit your question to provide a quick summary of what this code is *actually* supposed to do…? It’s not immediately apparent. Why do you open the file and loop it’s contents twice? Wouldn’t it be more efficient to loop it only once…? – esqew Dec 02 '21 at 01:09
  • Your code does exactly what you said. You read the whole file, but you overwrite your 7 variables each time, so when you exit the loop, all you have is the last. You should combine those two loops into one loop, so you do your computations as you read each line. Either that, or store all of the numbers into a list of tuples, then pass through the list of tuples. – Tim Roberts Dec 02 '21 at 01:12
  • The issue is that you first have a loop read all lines to industry's variable overwriting them at each step. Then you perform some computation. Just do everything in your first loop. – mozway Dec 02 '21 at 01:12
  • You need to merge the two loops into one. By the way, look at [using `with` when opening files](https://stackoverflow.com/a/3012565/1270789) as it simplifies code. – Ken Y-N Dec 02 '21 at 01:12
  • Edited it to clarify what I'm aiming for. Thanks for all the help! Tried merging the two loops, but still isn't working the way I hoped :( – Emma H Dec 02 '21 at 01:22

2 Answers2

0
for line in numbers:
    (m, a, e, i, o, O, E) = line.split(",")
    print(line)
    m = int(m)
    a = int(a)
    e = int(e)
    i = int(i)
    o = int(o)
    O = int(O)
    E = int(E)

Each time through this loop, you're throwing away the previous values of m, a, e, etc.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

This is what you intended:

numbers = open("Numbers.txt", "r")

calcresults = []

for line in numbers:
    (m, a, e, i, o, O, E) = line.split(",")
    print(line)
    m = int(m)
    a = int(a)
    e = int(e)
    i = int(i)
    o = int(o)
    O = int(O)
    E = int(E)
    x = m*2
    y = m+a
    z = m+E
    calcresults.append(x)
    calcresults.append(y)
    calcresults.append(z)

print(calcresults)

calcresults = str(calcresults)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30