forked from Nure/python-genius
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
66 lines (55 loc) · 1.5 KB
/
Copy pathfunction.py
File metadata and controls
66 lines (55 loc) · 1.5 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
def insert_user_in_database(name, age):
print(f"User inserted in DB for {name}, {age}")
# A function has to be called
insert_user_in_database("Alice", 34)
# Function returns (holds) value for later use
def insert_user_in_database(name, age):
return f"User inserted in DB for {name}, {age}"
print(insert_user_in_database("Alice", 34))
# --- Customer 1 (Texas) ---
amount1 = 1000
state1 = "TX"
if state1 == "TX":
tax_rate1 = 0.0625
elif state1 == "CA":
tax_rate1 = 0.0725
else:
tax_rate1 = 0.05
total_tax1 = amount1 * tax_rate1
print(f"Customer 1 Tax: ${total_tax1}")
# --- Customer 2 (California) ---
amount2 = 2500
state2 = "CA"
if state2 == "TX":
tax_rate2 = 0.0625
elif state2 == "CA":
tax_rate2 = 0.0725
else:
tax_rate2 = 0.05
total_tax2 = amount2 * tax_rate2
print(f"Customer 2 Tax: ${total_tax2}")
# --- Customer 3 (Other) ---
amount3 = 500
state3 = "NY"
if state3 == "TX":
tax_rate3 = 0.0625
elif state3 == "CA":
tax_rate3 = 0.0725
else:
tax_rate3 = 0.05
total_tax3 = amount3 * tax_rate3
print(f"Customer 3 Tax: ${total_tax3}")
# Function Way:
# Define the logic ONCE
def calculate_tax(amount, state):
if state == "TX":
tax_rate = 0.0625
elif state == "CA":
tax_rate = 0.0725
else:
tax_rate = 0.05
return amount * tax_rate
# Reuse it 50 times with a single line of code
print(f"Customer 1 Tax: ${calculate_tax(1000, 'TX')}")
print(f"Customer 2 Tax: ${calculate_tax(2500, 'CA')}")
print(f"Customer 3 Tax: ${calculate_tax(500, 'NY')}")