How to calculate a file of equations in python

J

John

Hi,
I have a text file which contains math expression, like this
134
+234
+234

(i.e. an operation (e.g. '+) and then a number and then a new line).

Can you please tell me what is the easiest way to calculate that file?
for example the above example should be = 134 + 234 + 234 = 602.

Thank you.
 
M

Michael Bentley

Hi,
I have a text file which contains math expression, like this
134
+234
+234

(i.e. an operation (e.g. '+) and then a number and then a new line).

Can you please tell me what is the easiest way to calculate that file?
for example the above example should be = 134 + 234 + 234 = 602.

Easiest?

eval(' '.join(open(fileName).readlines()).replace('\n',''))
 
P

Paul Rubin

John said:
I have a text file which contains math expression, like this
134
+234
+234

Erm, is this a homework assignment? Basicaly you need to identify all
the separate operands and operators and decide what to do with them.
I'm guessing you don't have to worry about operator precedence, for
example the rule that 2+3*5 is treated as 2+(3*5) because * has
precedence over +. Anyway the idea is keep a running total and
read through the input lines updating the total for each line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top