Skip to content

Commit 16d8457

Browse files
committed
Final QA
1 parent 4353417 commit 16d8457

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

python-while-loop/api_calls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
def is_api_available():
66
"""Simulate API availability."""
7-
time.sleep(1)
87
return random.choice([True, False, False, False])
98

109

@@ -19,6 +18,7 @@ def make_api_call(request):
1918
while True:
2019
if not is_api_available():
2120
print("API not available. Retrying in 1 sec...")
21+
time.sleep(1)
2222
continue
2323
make_api_call(request)
2424
try:

python-while-loop/for_loop.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
requests = ["first request", "second request", "third request"]
2+
3+
print("\nWith a for-loop")
4+
for request in requests:
5+
print(f"Handling {request}")
6+
7+
8+
print("\nWith a while-loop")
9+
it = iter(requests)
10+
while True:
11+
try:
12+
request = next(it)
13+
except StopIteration:
14+
break
15+
16+
print(f"Handling {request}")

python-while-loop/logfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
break
1313
print(f"Processing line content: {line_content}")
1414
else:
15-
time.sleep(1)
1615
print("No new content. Retrying in 1 second...")
16+
time.sleep(1)

python-while-loop/password.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
while True:
88
password = input("Password: ").strip()
9-
109
attempts += 1
11-
if attempts >= MAX_ATTEMPTS:
12-
print("Too many failed attempts.")
13-
break
1410

1511
if password == correct_password:
1612
print("Login successful! Welcome!")
1713
break
14+
15+
if attempts >= MAX_ATTEMPTS:
16+
print("Too many failed attempts.")
17+
break
1818
else:
1919
print(f"Incorrect password. {MAX_ATTEMPTS - attempts} attempts left.")

python-while-loop/process_pairs.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def process_pairs(sequence):
2+
"""Process a sequence, two elements at a time."""
3+
it = iter(sequence)
4+
while True:
5+
try:
6+
first = next(it)
7+
second = next(it)
8+
except StopIteration:
9+
break
10+
11+
yield first, second
12+
13+
14+
scientists = [
15+
"Newton",
16+
"Darwin",
17+
"Lovelace",
18+
"Freud",
19+
"Carver",
20+
"Curie",
21+
"Hopper",
22+
"Bohr",
23+
]
24+
25+
print("Pairs of scientists:")
26+
for first, second in process_pairs(scientists):
27+
print(f"- {first} and {second}")
28+
29+
30+
print("\nWith zip()")
31+
for first, second in zip(scientists[::2], scientists[1::2]):
32+
print(f"- {first} and {second}")

python-while-loop/temperature.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44

55
def read_temperature():
6-
time.sleep(1)
76
return random.uniform(20.0, 30.0)
87

98

109
while True:
1110
temperature = read_temperature()
1211
print(f"Temperature: {temperature:.2f}°C")
1312

14-
if temperature >= 25:
13+
if temperature >= 28:
1514
print("Required temperature reached! Stopping monitoring.")
1615
break
16+
17+
time.sleep(1)

0 commit comments

Comments
 (0)