i've been training in python for a month now (i'm still noobish), and i'm with this project where i want to create calculator using python handlin (.txt files).
I know how to create a calculator (program) in python, no problems there, but i've encountered problem using .txt files.
So, i have .txt file called expression.txt that contains following:
9-1
6-3
So, it consists out of a number (9), operator (minus -), and number again (1).
I need to create a program that reads this expressions, and writes it and its result in a seperate file called result.txt
result.txt should look like this:
9-1=8
6-3=3
This is where i'm stuck, so im really hopeing that someone could give me a push.
class Calculator:
def sub(self,a,b):
return a - b
with open('./expression.txt', 'r') as f:
lines = f.readlines()
for l in lines:
if l[1] == '-':
print(Calculator.sub(int(l[0]), int(l[2])))
with open('./result.txt', 'w') as f2:
f2.write('Test')
with open('./result.txt', 'r') as f3:
print(f3.read())
Thanks lads!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…