-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrrorhandling.py
More file actions
68 lines (51 loc) · 2.38 KB
/
Copy pathErrrorhandling.py
File metadata and controls
68 lines (51 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
print("=== STORE CHECKOUT SYSTEM ===\n")
# --- 1. ValueError & TypeError ---
# Scenario: Converting user text input to an integer or mixing incompatible types
try:
raw_quantity = input("Enter item quantity to purchase: ")
quantity = int(raw_quantity) # Raises ValueError if input is not a number
# total_price = "10.0" + 5 # Would raise TypeError (un-comment to test)
total_price = quantity * 10.0
print(f"Subtotal for {quantity} items: ${total_price:.2f}\n")
except ValueError:
print("❌ ValueError: Invalid input! Quantity must be a whole number.\n")
quantity = 0 # Default value so the rest of the script doesn't crash
except TypeError:
print("❌ TypeError: Cannot combine incompatible data types.\n")
quantity = 0
# --- 2. ZeroDivisionError ---
# Scenario: Splitting a fixed discount across items when quantity is 0
try:
discount = 50.0
discount_per_item = discount / quantity # Raises ZeroDivisionError if quantity == 0
print(f"Discount applied per item: ${discount_per_item:.2f}\n")
except ZeroDivisionError:
print("❌ ZeroDivisionError: Cannot split a discount across 0 items!\n")
# --- 3. IndexError ---
# Scenario: Accessing a list element with an out-of-bounds index
promo_gifts = ["Keychain", "Sticker", "Tote Bag"]
try:
choice_index = int(input("Pick a promo gift index (0, 1, or 2): "))
selected_gift = promo_gifts[choice_index] # Raises IndexError if index is 3 or higher
print(f"You selected: {selected_gift}\n")
except IndexError:
print("❌ IndexError: Invalid choice! That item index does not exist in the list.\n")
except ValueError:
print("❌ ValueError: Gift index must be a valid number.\n")
# --- 4. PermissionError ---
# Scenario: Attempting to save a file in a restricted system folder
try:
# Trying to open a system file location that lacks user write permissions
with open("/system_receipt_test.txt", "w") as file:
file.write("Receipt Data")
except PermissionError:
print("❌ PermissionError: Access denied! Cannot write to a protected directory.\n")
# --- 5. NameError ---
# Scenario: Calling a variable that was never declared or misspelled
try:
# 'receipt_id' was never defined above
print(f"Receipt ID: {receipt_id}\n")
except NameError:
print("❌ NameError: Variable 'receipt_id' is not defined anywhere in the script.\n")
print("=== Checkout Process Finished ===")