Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python-while-loop/api_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

def is_api_available():
"""Simulate API availability."""
time.sleep(1)
return random.choice([True, False, False, False])


Expand All @@ -19,6 +18,7 @@ def make_api_call(request):
while True:
if not is_api_available():
print("API not available. Retrying in 1 sec...")
time.sleep(1)
continue
make_api_call(request)
try:
Expand Down
16 changes: 16 additions & 0 deletions python-while-loop/for_loop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
requests = ["first request", "second request", "third request"]

print("\nWith a for-loop")
for request in requests:
print(f"Handling {request}")


print("\nWith a while-loop")
it = iter(requests)
while True:
try:
request = next(it)
except StopIteration:
break

print(f"Handling {request}")
2 changes: 1 addition & 1 deletion python-while-loop/logfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
break
print(f"Processing line content: {line_content}")
else:
time.sleep(1)
print("No new content. Retrying in 1 second...")
time.sleep(1)
8 changes: 4 additions & 4 deletions python-while-loop/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

while True:
password = input("Password: ").strip()

attempts += 1
if attempts >= MAX_ATTEMPTS:
print("Too many failed attempts.")
break

if password == correct_password:
print("Login successful! Welcome!")
break

if attempts >= MAX_ATTEMPTS:
print("Too many failed attempts.")
break
else:
print(f"Incorrect password. {MAX_ATTEMPTS - attempts} attempts left.")
32 changes: 32 additions & 0 deletions python-while-loop/process_pairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def process_pairs(sequence):
"""Process a sequence, two elements at a time."""
it = iter(sequence)
while True:
try:
first = next(it)
second = next(it)
except StopIteration:
break

yield first, second


scientists = [
"Newton",
"Darwin",
"Lovelace",
"Freud",
"Carver",
"Curie",
"Hopper",
"Bohr",
]

print("Pairs of scientists:")
for first, second in process_pairs(scientists):
print(f"- {first} and {second}")


print("\nWith zip()")
for first, second in zip(scientists[::2], scientists[1::2]):
print(f"- {first} and {second}")
5 changes: 3 additions & 2 deletions python-while-loop/temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@


def read_temperature():
time.sleep(1)
return random.uniform(20.0, 30.0)


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

if temperature >= 25:
if temperature >= 28:
print("Required temperature reached! Stopping monitoring.")
break

time.sleep(1)