Reputation: 3066
I have the following python code for an if,elif,else statement:
if line_num == 151:
if self.run_count == 1:
print(values[self.run_count-1])
elif line_num == 129:
if self.run_count == 2:
print(values[self.run_count-1])
elif line_num == 129:
if self.run_count == 3:
print("here")
else:
print(line_num)
f.write(line)
The code executes correctly for the first if and elif statement. However You can see by this output that one the third run of the code when the statement goes into the 3rd elif statement (where the run count is three) it executes the statement but does not print out anything, and does not execute the else statement. I have checked if the run_count was indeed 3 and it was so that was not throwing my program off at all.
Does anyone have an idea while it may go into that elif statement but never print anything out, when the previous one works correctly and all the conditions are met?
Upvotes: 3
Views: 3041
Reputation: 44161
Both elif line_num == 129
statements will not get executed because they have the same condition. Instead, try something like this:
if line_num == 151:
if self.run_count == 1:
print(values[self.run_count-1])
elif line_num == 129:
if self.run_count == 2:
print(values[self.run_count-1])
elif self.run_count == 3:
print("here")
else:
print(line_num)
f.write(line)
Upvotes: 15