0

I am trying to write a program that solves simple math equations from a txt file and puts the answers in a different txt file. For example:

qustions.txt:

1+2=
4+7=
10*2=
10/2=

And then the answers will be in a different txt file

answers.txt:

1+2=3
4+7=11
10*2=20
10/2=5

So there are simple math equations in a text file and the answers in a different one. The math equations are only number - operator - number

Chelmy88
  • 1,106
  • 1
  • 6
  • 17

2 Answers2

0

You can use eval to evaluate everything to the left of the equal sign.

with open('questions.txt') as fp:
    qs = fp.readlines()

answers = [eval(q.split('=')[0]) for q in qs if q.strip()]

with open('answers.txt', 'w') as fp:
    for q, a in zip(qs, answers):
        fp.write(q.strip() + str(a) + '\n')

answers is a list of the evaluated expressions on the left side of the equal sign. eval takes whatever string is given to it and tries to run it as a command in Python. q.split('=')[0] splits each question into two parts: everything to the left of the equal sign (part 0) and everything to the right (part 1). We are grabbing only the first part to evaluate. The rest of the line is iterating over the questions in your file and checking to make sure the line is not just a extra blank line.

Using zip matches up each question q to the corresponding answer a, so the for loop yield both the first q and a, then second q and a, etc. fp is a file object that we opened for writing. fp.write tells python to write to disk the string argument. I am using q.strip() to remove the newline characters, appending the answer as a string, and then adding a newline character to the end.

James
  • 32,991
  • 4
  • 47
  • 70
  • what is this fp.write? it gives me an error as well –  Aug 22 '19 at 10:02
  • oops. literal_eval should be eval – James Aug 22 '19 at 10:02
  • what version of Python are you using? this only works for 3.6 or higher – James Aug 22 '19 at 10:04
  • Oooo I am using 2.7. But can you explain the last 2 lines? –  Aug 22 '19 at 10:05
  • Ok. Updated to remove f-string reference. Also, switch to python 3. – James Aug 22 '19 at 10:06
  • yeah it works can you just explain "answers" and the last 2 lines? –  Aug 22 '19 at 10:09
  • 1
    Ok, added more explanation. For more details you should really look into a introduction to Python. All of the functionality here is fairly basic. – James Aug 22 '19 at 10:17
  • Can you explain the zip again? I am not sure that I got that –  Aug 22 '19 at 10:41
  • And yet I still don't understand why would you use zip what does it print? the answer? like because of that you can print a which 'a' is the answer, but I still don't understand why –  Aug 22 '19 at 11:18
0

That's how I ended up doing it:

with open('questions.txt') as fp:
    qs = fp.readlines() # reading the qustion file
with open('answers.txt', 'w') as fp:# writing the text file by the name fp
    for q in qs:
        Deleteequal = q.split('=')
        a = eval(Deleteequal[0]) # always going to be line 0 because I am reading a line by line
        f = q + str(a)
        f = f.replace("\n", "") # for some reason it printed the numbers really weird if I've just did f.write(q+str(a)+'\n') the result would be 1 line down for some reason
        fp.write(f)
        fp.write('\n')
# str(a) to write the final answer