ICT582 Python Programming Principles and Practice
– 1 –
Python Lab 7
LEARNING OBJECTIVES
•Understand exception handling in Python.
SUBMISSION GUIDELINE
• Submit a single word document (in the appropriate link in LMS)
with following guidelines:
o Cover page: Write your ID, Name, Lab number etc
o For every question in the lab:
➢ Code: Copy and paste the exact code from your python file. Please
note – this is not a snapshot. This is the actual code. I should be able
to copy your code from here to run in my PyCharm.
➢ Screenshots: Some input-output screenshots to reflect different sets
of inputs and output.
Note: Marks will be deducted if you do not follow the exact guideline
Exception Handling Example: Please read carefully.
Consider the following function which divides a number by zero;
def calc(x):
x / 0
This will raise an error when it is run for any number: If we now call this function,
we will get the error trackback in the standard output:
calc(6)
However, we can handle this by wrapping the call to runcalc within a try
statement and providing an except clause. The syntax for a try statement with
an except clause is:
ICT582 Python Programming Principles and Practice
– 2 –
try:
calc(6)
except:
print(‘oops’)
which now results in the string ‘oops’ being printed out. This is because when
runcalc is called the ‘/’ operator throws the ZeroDivisionError which is
passed back to the calling code which has an except clause specifying this type
of exception. This catches the exception and runs the associated code block
which in this case prints out the string ‘oops’.
Here’s an alternative way to write the above code:
try:
calc(6)
except ZeroDivisionError:
print(‘oops’)
Q1. Write a program that accepts two integer inputs from the user and checks if
one integer evenly divides another. In your solution, you are required to
implement exception handling to check user inputs for validity and also to avoid
ZeroDivisionError in the output.
You may use a Python function exit(0), which exits the program immediately,
skipping anything that comes after it.
Q2. You are in country where there are two conditions for fishing:
(a) you can fish if you are 17 years old or less and your parent has a license.
(b) If you are 18 years old or more you need to have your own license.
Write a program that tells someone if they are legal to fish. First ask them how
old they are, whether they have a license or not, and whether their parent has a
license or not. Add exception handling to the program so that if the user answers
something other than their age that the program prints “You did not enter your
age correctly”.
ICT582 Python Programming Principles and Practice
– 3 –
Q3. Consider the following program (an example of continuous menu)
D=[‘Sara’, 3.40, ‘04235’,’IT’]
def runMenu():
choice=1
while not choice==0:
print(“Please enter your choices:”)
print(“1- to print your Name”)
print(“2- to print your GPA”)
print(“3- to print your Contact”)
print(“4- to print your major”)
print(“0- to Exit”)
choice=int(input())
print(D[choice-1])
runMenu()
Questions:
a. List all the errors might happen due to some unexpected inputs
b. Fix them using exception handling concept