OOPwithPythonLecture5 1
OOPwithPythonLecture5 1
WITH PYTHON
Second Class
1st Semester
Bug-busting checklist
Exceptions
Benefits of OOP
BUG-BUSTING CHECKLIST
Sometimes you might think that you’ll never get a program to work, but don’t give up! If
you follow the tips in this handy checklist, you’ll be able to identify most errors.
Ask yourself...
• If you build one of the projects in one of the books and it doesn’t work, check that the
code you’ve typed matches the book exactly.
• Is everything spelled correctly?
• Do you have unnecessary spaces at the start of a line?
• Have you confused any numbers for letters, such as 0 and O?
• Have you used upper-case and lower-case letters in the right places?
• Do all open parentheses have a matching closing parenthesis? ( ) [ ] { }
• Do all single and double quotes have a matching closing quote? ‘ ‘ “ ”
• have you checked the name of the python file that you are currently running?
EXCEPTIONS
Exceptions are errors that crash our programs. They often happen because of bad input or
programming errors. It’s our job to anticipate and handle these exceptions to prevent our
programs from crashing.
age = int(input("Enter your age: "))
print(age)
Enter your age : 25
25
try:
age = int(input("Age: "))
print(age)
except ValueError:
print("Not a valid number")
Notice that we have to use the same error message that appears (ValueError)
EXCEPTIONS
If we have a mathematical operations in our code like a DIVISION we have to think
about the division by zero error
try:
age = int(input("Age: "))
income = 20000
risk = income / age
print(age)
except ValueError:
print("Not a valid number!")
Enter your age : 0
try:
age = int(input("Age: "))
income = 20000
risk = income / age
print(age)
except ValueError:
print("Not a valid number")
except ZeroDivisionError:
print("Age cannot be 0")
PROCEDURAL PROGRAMING CONCEPT
In procedural programming, the program divided into a set of function, so we have
data stored in a set of variables and functions that operate on the data.
f() X
f() X
This style of programming is very simple and straightforward, but after your programs
grow it will end up with a lot of functions .
You might find yourself coping and pasting lines of code over and over
PROCEDURAL PROGRAMING CONCEPT
If you make change on one function, several other functions breaks.
f() f()
f() f()
f() f()
Property
f() X
Method
f() X
OBJECT ORIENTED PROGRAMING CONCEPT
As an Example we can think in a Car, a car is an object with
properties such as (make, model, color)
Methods like (start, stop and move) CAR
Make
Model
Grouping related variables and functions that operates on them into object Color
is called encapsulation
Start()
Benefits of encapsulation Stop()
1- simpler interface (no too many parameters). Move()
Click()
Focus()