-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_flow_exercises.py
More file actions
98 lines (81 loc) · 3.14 KB
/
Copy pathcontrol_flow_exercises.py
File metadata and controls
98 lines (81 loc) · 3.14 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# ==========================================
# Python Learning Lab: 02 Control Flow
# ==========================================
print("--- Running Control Flow Exercises ---")
# ==========================================
# Challenge 1: Conditionals (if-elif-else)
# ==========================================
# Write a function that returns a letter grade based on a score:
# - Score 90 and above: "A"
# - Score 80 to 89: "B"
# - Score 70 to 79: "C"
# - Score 60 to 69: "D"
# - Score below 60: "F"
def get_grade(score):
# TODO: Implement score classification logic
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
# --- Verification ---
assert get_grade(95) == "A", "95 should be an A"
assert get_grade(80) == "B", "80 should be a B"
assert get_grade(72) == "C", "72 should be a C"
assert get_grade(60) == "D", "60 should be a D"
assert get_grade(45) == "F", "45 should be an F"
print("✓ Challenge 1 passed!")
# ==========================================
# Challenge 2: Loops (for, while)
# ==========================================
# Write a function that calculates the sum of all EVEN numbers in a range (inclusive).
# For example: sum_evens(1, 10) should add 2 + 4 + 6 + 8 + 10 = 30.
def sum_evens(start, end):
total = 0
# TODO: Implement the loop to sum even numbers
for num in range(start, end + 1):
if num % 2 == 0:
total += num
return total
# --- Verification ---
assert sum_evens(1, 10) == 30, f"Expected 30, got {sum_evens(1, 10)}"
assert sum_evens(4, 4) == 4, f"Expected 4, got {sum_evens(4, 4)}"
assert sum_evens(3, 7) == 10, f"Expected 10, got {sum_evens(3, 7)}"
print("✓ Challenge 2 passed!")
# ==========================================
# Challenge 3: Break & Continue
# ==========================================
# Complete the function below to return a list of numbers from `num_list`
# but stop adding numbers once you encounter a negative number (using `break`).
# Also skip any number that is equal to 0 (using `continue`).
def process_numbers(num_list):
result = []
# TODO: Implement loop with break and continue
for num in num_list:
if num < 0:
break
if num == 0:
continue
result.append(num)
return result
# --- Verification ---
test_list = [3, 0, 5, 8, -1, 10, 2]
assert process_numbers(test_list) == [3, 5, 8], f"Expected [3, 5, 8], got {process_numbers(test_list)}"
print("✓ Challenge 3 passed!")
# ==========================================
# Challenge 4: List Comprehension
# ==========================================
# Generate a list of squares of ODD numbers from the input list `numbers`.
# Example: [1, 2, 3, 4, 5] -> [1, 9, 25] (since odd numbers are 1, 3, 5)
def odd_squares(numbers):
# TODO: Implement using a list comprehension
return [n ** 2 for n in numbers if n % 2 != 0]
# --- Verification ---
assert odd_squares([1, 2, 3, 4, 5, 6]) == [1, 9, 25], f"Expected [1, 9, 25], got {odd_squares([1, 2, 3, 4, 5, 6])}"
print("✓ Challenge 4 passed!")
print("\n🎉 All tests passed successfully!")