[Solved] Why is this basic python program not working?


Your problem is this line

subbed = line[-3:]

This returns the following:

ok\n

or

il\n

Thus, your if statement checking if subbed == 'ok' will fail every time.

You can fix this by only taking the last two characters, after stripping the newline:

subbed = line.strip()[-2:]

My original answer didn’t account for reading from a file. That answer assumed that the results would be either ,ok or ail. This is not the correct response when reading from a file. The answer above has been modified to show what will be returned and adjusts the solution appropriately.

3

solved Why is this basic python program not working?