Me again. I am determined to make my way through these lessons, but this is, honestly, a bit grueling. It might be helpful for someone (other than me) to take a pass through these lessons.
On this lesson:
the comments above the two write lines have rows and columns transposed. Swapping out 0,0 for 4,0 would be column 4, row 0 NOT row 4, column 0 as indicated in the comment line
in the except code block on the website lesson you have
except KeyboardInterrupt:
# Clear the LCD display if a keyboard interruption (e.g., Ctrl+C) occurs
LCD1602.clear()
pass # Proceed with no further action
The # Clear comment and the LCD1602.clear() lines are NOT in the file that is downloaded for the Pi5.
But ALSO - LCD1602.clear() does not do anything from its position in the except block (but does work if you move it to the last line of the setup block. I am not a programmer (but I am trying to learn this stuff) - I think it is because you never GET to end the program with a ctrl-c as it isn’t in a while loop - it just runs the once and it is done. Could use some confirmation on that.
Well, I suppose it is possible you could Ctrl-C before the two seconds are up…
So I moved the clear code into a separate destroy() that is called both as the last line of the setup code and is in the except code. Do I still need pass for that?
Thank you for your detailed feedback – you are a very attentive learner, and we appreciate you taking the time to share your thoughts.
About row and column labelling
You are right – the labels could be clearer. We will improve the annotations in the next update.
About try-except
try-except is used to catch and handle exceptions that occur during program execution. For example, except KeyboardInterrupt: catches the exception triggered when the user manually stops the program (by pressing Ctrl+C).
About pass
pass is a placeholder statement that does nothing. It is commonly used when you haven’t decided what to write yet, or when you need to keep the syntax structure intact to avoid errors. Once you complete the code, keeping or removing pass does not affect the program’s functionality.
Why try-except and pass might seem unnecessary
In small example scripts, they may appear redundant. However, in large‑scale software development, developers often use a “skeleton‑first, flesh‑later” approach. They define the structure of modules, classes, and functions, and use try-except and pass as placeholders for exception handling – this allows the overall structure to be debugged without causing syntax errors, even when parts of the code are not yet implemented.
We hope this clarifies your questions. If you have any further thoughts, feel free to share!
As this code only runs for 2 seconds, it is most likely that it will have already run before the operator even gets a chance to do a Ctrl-C. If that is the case, Ctrl-C does nothing (as the program has already completed) and the current text on the display does NOT get cleared. The only way to clear it, at that point, (at least that I found) is to remove the power jumper wire.
That’s why I was suggesting adding a destroy statement that could be run both at the end of the program AND as part of an except statement.
You are correct. In the current example, the program exits normally after the 2-second delay, so KeyboardInterrupt is not triggered and LCD1602.clear() is not executed. As a result, the text remains on the LCD.
A better approach is to use finally, so the display is cleared whether the program finishes normally or is interrupted:
try:
setup()
except KeyboardInterrupt:
pass
finally:
LCD1602.clear()
Thank you for pointing this out! We’ll update the example to make this behavior clearer.