Programming
Construction Checklist
| Practice
Today Task
Programming Construction Checklist
Clear naming conventions
Proper indentation & comments
Error handling (try/except)
Modular functions
Code reuse
Testing & debugging
User-friendly design
Documentation
Agenda
- Tkinter Widgets Overview For making GUI
- Try/Except Error Handling
- Naming Conventions
- Testing Code
- Programming Checklist
- Class Assignment
User Friendly GUI Widgets
(Ref Material)→LAB 4 Slides
1. Tk() → Create main window
2. Label → Show text ,images
3. Entry → get input from users
4. Button → Performing Actions
5. MessageBox → Show
messages,alerts,warnings
[Link]→ set Widgets positons
Error Handling with Try/Except
In programming, try and except are used for error handling.
try: block = contains code that might cause an error.
except: block = contains code that runs only if an error
happens, so the program does not crash.
Think of it like:
try → "Try to run this code."
except → "If something goes wrong, catch the error and
handle it safely."
Exception Classes in Python
BaseException
└── Exception
├── ValueError
├── TypeError
├── ZeroDivisionError
├── IndexError
├── KeyError
├── FileNotFoundError
[Link] – Wrong value
try:
num = int("abc") # can't convert text to number
except ValueError:
print("Caught ValueError → Wrong value for conversion")
2. TypeError – Wrong type of operation
try:
result = "abc" + 5 # string + int not allowed
except TypeError:
print("Caught TypeError → Operation on wrong types")
3. ZeroDivisionError – Dividing by zero
try:
result = 10 / 0
except ZeroDivisionError:
print("Caught ZeroDivisionError → Division by zero not possible")
4. IndexError – List index out of range
try:
items = [1, 2, 3]
print(items[5]) # index does not exist
except IndexError:
print("Caught IndexError → List index not found")
5. KeyError – Dictionary key not found
try:
data = {"name": "Ali"}
print(data["age"])
except KeyError:
print("Caught KeyError → Dictionary key missing"
6. FileNotFoundError – File does not exist
try:
f = open("[Link]")
except FileNotFoundError:
print("Caught FileNotFoundError → File not found")
Naming Conventions
Case Style Example Use For
snake_case user_name Variables, functions
PascalCase BankAccount Classes
UPPER_CASE MAX_LIMIT Constants
Testing Example
Manual Test:
1. Enter 500 → Deposit → Balance updates
2. Enter text → Deposit → Error shown
3. Withdraw > balance → Warning shown
Unit Test Example (pytest): def add(a, b):
return a + b
def test_deposit():
balance = 1000 # Test cases
assert add(2, 3) == 5
result = deposit(200)
assert add(-1, 1) == 0
assert result == 1200 print("All tests passed!")
Class Assignment
Tasks:
. Take the provided poor code like ATM, Calculator, Age calculator Food
Menu Etc.
Apply the checklist:
User Friendly GUI
Fix naming
Add try/except
Add modular functions
Improve messages
Test with valid/invalid input
Goal: Convert poor code → quality GUI code.