-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path02_while_loop.py
More file actions
263 lines (210 loc) · 6.88 KB
/
02_while_loop.py
File metadata and controls
263 lines (210 loc) · 6.88 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"""
================================================================================
File: 02_while_loop.py
Topic: While Loops in Python
================================================================================
This file demonstrates the 'while' loop in Python, which repeatedly executes
a block of code as long as a condition remains True. Unlike for loops,
while loops are typically used when the number of iterations is not known
in advance.
Key Concepts:
- Basic while loop syntax
- Counter-controlled loops
- Sentinel-controlled loops (input validation)
- Infinite loops and how to handle them
- While-else construct
================================================================================
"""
# -----------------------------------------------------------------------------
# 1. Basic While Loop
# -----------------------------------------------------------------------------
# Executes as long as condition is True
print("--- Basic While Loop ---")
count = 1
while count <= 5:
print(f"Count: {count}")
count += 1 # Don't forget to update the condition variable!
print("Loop finished!")
# -----------------------------------------------------------------------------
# 2. Counter-Controlled While Loop
# -----------------------------------------------------------------------------
# When you know how many iterations you need
print("\n--- Counter-Controlled Loop ---")
# Countdown
countdown = 5
print("Rocket Launch Countdown:")
while countdown > 0:
print(f"T-minus {countdown}...")
countdown -= 1
print("Liftoff! 🚀")
# -----------------------------------------------------------------------------
# 3. Summing Numbers with While
# -----------------------------------------------------------------------------
# Accumulator pattern
print("\n--- Sum of Numbers ---")
# Sum numbers from 1 to 10
n = 1
total = 0
while n <= 10:
total += n
n += 1
print(f"Sum of 1 to 10 = {total}")
# -----------------------------------------------------------------------------
# 4. Sentinel-Controlled Loop (Input Validation)
# -----------------------------------------------------------------------------
# Loop until a specific condition is met
print("\n--- Sentinel-Controlled Loop ---")
# Simulating password validation (in practice, use input())
attempts = 0
max_attempts = 3
correct_password = "secret123"
# Simulated user inputs
user_inputs = ["wrong1", "wrong2", "secret123"]
while attempts < max_attempts:
# In real code: password = input("Enter password: ")
password = user_inputs[attempts]
print(f"Attempt {attempts + 1}: Entered '{password}'")
if password == correct_password:
print("Access granted! ✓")
break
else:
print("Incorrect password.")
attempts += 1
else:
# This runs if the loop completes without break
print("Too many failed attempts. Account locked.")
# -----------------------------------------------------------------------------
# 5. While Loop with User Simulation
# -----------------------------------------------------------------------------
# Menu-driven program example
print("\n--- Menu-Driven Example ---")
# Simulated menu choices
menu_selections = [1, 2, 4, 3]
selection_index = 0
running = True
while running:
# Simulating menu selection
choice = menu_selections[selection_index]
selection_index += 1
print(f"\nMenu: 1=Add, 2=View, 3=Exit | Choice: {choice}")
if choice == 1:
print(" ➤ Adding item...")
elif choice == 2:
print(" ➤ Viewing items...")
elif choice == 3:
print(" ➤ Exiting program...")
running = False
else:
print(" ➤ Invalid choice, try again.")
# -----------------------------------------------------------------------------
# 6. Infinite Loops (and how to avoid them)
# -----------------------------------------------------------------------------
# A loop that never ends - usually a bug
print("\n--- Avoiding Infinite Loops ---")
# BAD: This would run forever (commented out)
# i = 0
# while i < 5:
# print(i)
# # Missing: i += 1
# GOOD: Always update the condition variable
i = 0
while i < 5:
print(f"i = {i}")
i += 1 # This is crucial!
# -----------------------------------------------------------------------------
# 7. While-Else Construct
# -----------------------------------------------------------------------------
# The else block executes if the loop completes normally (no break)
print("\n--- While-Else Construct ---")
# Example 1: Loop completes normally
n = 5
while n > 0:
print(n, end=" ")
n -= 1
else:
print("\n ↳ Countdown complete! (else block executed)")
# Example 2: Loop exits via break
print("\nSearching in list:")
items = ["a", "b", "c", "d"]
target = "b"
index = 0
while index < len(items):
if items[index] == target:
print(f" Found '{target}' at index {index}")
break
index += 1
else:
print(f" '{target}' not found")
# -----------------------------------------------------------------------------
# 8. Nested While Loops
# -----------------------------------------------------------------------------
# A while inside another while
print("\n--- Nested While Loops ---")
# Create a simple pattern
row = 1
while row <= 4:
col = 1
while col <= row:
print("*", end=" ")
col += 1
print() # New line
row += 1
# -----------------------------------------------------------------------------
# 9. While with Break and Continue
# -----------------------------------------------------------------------------
# Control flow within loops (preview - detailed in next file)
print("\n--- Break and Continue in While ---")
# Break example
print("Break example (stop at 5):")
i = 0
while True: # Intentional infinite loop
i += 1
if i > 5:
break
print(i, end=" ")
print()
# Continue example
print("\nContinue example (skip even numbers):")
i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue # Skip even numbers
print(i, end=" ")
print()
# -----------------------------------------------------------------------------
# 10. Practical Examples
# -----------------------------------------------------------------------------
print("\n--- Practical Examples ---")
# Fibonacci sequence
print("Fibonacci sequence (first 10 numbers):")
a, b = 0, 1
count = 0
while count < 10:
print(a, end=" ")
a, b = b, a + b
count += 1
print()
# Finding digits in a number
print("\nDigits of 12345:")
number = 12345
while number > 0:
digit = number % 10
print(f" Digit: {digit}")
number //= 10
# Guessing game logic
print("\n Guess the Number Game Logic:")
secret = 7
guesses = [3, 8, 5, 7]
guess_index = 0
while guess_index < len(guesses):
guess = guesses[guess_index]
print(f" Guess: {guess}", end=" ")
if guess < secret:
print("- Too low!")
elif guess > secret:
print("- Too high!")
else:
print("- Correct! 🎉")
break
guess_index += 1