Reputation:
I have been experimenting with files in Python. I made three:
156
in it 45
in it++&/**
in it because I want to see if you can add through a fileFilePath_file1 = r'D:\python_CSV\Number1.txt'
FilePath_file2 = r'D:\python_CSV\Number2.txt'
FilePath_fileOP = r'D:\python_CSV\Op.txt'
File1 = open(FilePath_file1,'r')
File2 = open(FilePath_file2,'r')
FileOp = open(FilePath_fileOP,'r')
Number1 = File1.readline()
Number2 = File2.readline()
OpCommand = FileOp.readline()
x = int(Number1)
y = int(Number2)
z = -1
if OpCommand == '+':
z = x + y
print('The result is:- ',z)
The code keeps returning -1 instead of 201, which is what the answer should be. Why is that?
Upvotes: 0
Views: 37
Reputation: 5190
When you read in your value for OpCommand
it is the whole line ++&/**
, so matching against the single character +
will return false
. Hence z
is never modified. Even if you have the operator characters on separate lines, readline()
includes the newline character, so you need to either remove it or just use OpCommand[0]
to match. Replace
if OpCommand == '+':
with
if OpCommand[0] == '+':
Upvotes: 2