A very simple example of a program that might experience this problem is below but also applied to any program that needs a number as an input (which by default, Python might treat as a String and so the programmer will explicitly tell Python that the inputted variable is an integer or a decimal number (float)
Below is a program that shows this problem, where users if input data that is the wrong type, the whole program will crash:
Luckily Python has some ways to handle any unexpected errors in your code and If you have code that may raise an exception (and crash the whole program), you can place that code in a try: block
After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible (shows that there has been an error or the user might not notice) and the rest of the program will still continue to run.
Here's the example of the code modified to use the try and except blocks. I have added a line at the end of the program to prove that the program has continued to run successfully.
The code will take the input from the user and if this input is correct (an input that does not crash the program i.e. a number) then the Except part will be skipped.
If the input causes an exception, then the code in the except block will be executed (which gives a chance for the user to correct the mistake).
We need to add an Indefinite Loop to not allow the user past this input unless they enter something that the computer can handle. In Python, we are going to use a while loop with the Try / Except inside it and this is how it should look (below).
It is worth noting a couple of changes now.
1) we have removed the input from the except part of the loop as this would mean that this would be executed and then also the part in the try (again) once the loop executes back to its beginning.
2) we have added a break to the try which will mean that the loop is exited once data has been successfully entered.
This below is an example of the output showing how the program now handles multiple entries of data what would have caused the whole program to crash before:
For other ideas click here