Session 4 - Part 1 Flashcards
Broadly, what are the two types of errors in programming? - (2)
- Errors that stop you running the code (‘syntax errors’) - These are often easy to spot. These days the editor will usually show you where they are.
- Errors that happen while you are running the code - These are the ones that keep you up at night. Some of them crash the programme. That is good! The other ones just do the wrong thing but don’t throw an error. That is bad! -runtime errors
Provide an example of syntax error - (2)
print(‘Welcome to the programming course!)
Unterminated string literal
What are syntax errors in programming?
A syntax error occurs when the structure of the code does not conform to the rules of the programming language, making it invalid and preventing the code from being executed. - error in the way code has been written
Example of an error that only pops up when you are running your code - (3)
myNum = int(input(‘Enter a number’))
Giving input as string instead of a number
Running this code will produce an ValueError exeception as Python does not know what ‘ten’ is
What is ValueError exception in Python?
A ValueError exception occurs in Python when a function expects a certain type of input, such as a string or a number, but the actual value provided is not valid or appropriate for that type.
Another example of ValueError exception:
If you try to convert a string containing non-numeric characters into an integer using the int() function, like int(“abc”), Python will raise a ValueError because it can’t interpret “abc” as a valid number.
What is an exception?
An error that occurs while the code is running.
What does a traceback in Python indicate? - (2)
traceback in Python indicates that an exception has occurred during the program’s execution.
It provides information about where the exception happened in the code, often marked by a little arrow or a message like ‘An error occurred on line xx’.
Whenever you get a traceback from Python, it means that something
unexpected has happened in your program.
The exact line number in your traceback may differ depending on how you have structured your script, but the
message will be similar.
If you are running the script in Spyder, the traceback will be in red. In Colab it will have a
variety of colours.
How should tracebacks be typically read in Python? - (2)
Tracebacks should generally be read from bottom to top.
e.g., Value Error: invalid literal (i.e., thingy) for int() for base 10: ‘ten’
In this traceback, it has given
Python gives us a hint as to both the error (ValueError: invalid literal…) and the line of code in which the error occurred (in fact it gives us both the file and line number as well as the code from the line).
What does the error message “Invalid literal for int() with base 10” mean? - (2)
This error message indicates that Python attempted to convert a string into an integer (of base 10 – normal decimal numbers), but the string doesn’t resemble a number.
This is not something which can be done, so Python raises an ValueError exception
What is this error message “Invalid literal for int() with base 10” due to?
Users often provide unexpected inputs, leading to exceptions.
Why write dumb code? (code that is simple and logical) - (2)
- Will make debugging your code easier
- You can always ask AI to make your ‘dumb code’ smarter or more optimised but if you start with ‘complex’ code then it is harder to fix it
To deal with an exception we can use the
try and except keywords
Describe in more detail about the try and except blocks
try means that we will attempt the code within the block and, if an exception is found, we will move to the except block (or blocks).
Describe the purpose of the try-except block in the provided example - (2)
The try-except block attempts to convert user input into an integer.
If a ValueError exception occurs (if the input is not a number), the except block handles it by printing a message and the exception itself (e is returned as the exception)
What is the output of this code - if input is ‘hi’
Note when an exception is caught and handled using a try-except block, we do not get a - (2)
Note that we no longer get a full traceback.
Instead, it executes the code in the except block, which typically includes a message and the error message itself (ValueError exception) and the programme stops but critically it does not crash.
How to extend this code in which we should just ask again until the user gives us something sensible.
You can use try and except blocks if you suspect code may
crash
Explain this code - (7)
This code initializes the variable start
to data type None
, indicating that no valid value has been chosen yet by the user.
The while loop continues executing as long as start
remains None
, prompting the user for input.
Inside the loop,
the try block utilises input function to get a value from user and convert it into int().
If a ValueError occurs (indicating invalid input), it moves to except block where code inside is executed which is
an error message is printed (Not a number), and the loop continues and goes from the start again.
Once a valid integer is entered (e.g,, 1) returning to the start of the while loop.
Since start is now updated with a valid integer, it is no longer None, and the loop exits at the bottom.”, printing “Thanks!”.
This ensures the user provides a valid integer input before proceeding further.