diff --git a/python-while-loop/api_calls.py b/python-while-loop/api_calls.py index bc3747e038..f221f5c12a 100644 --- a/python-while-loop/api_calls.py +++ b/python-while-loop/api_calls.py @@ -4,7 +4,6 @@ def is_api_available(): """Simulate API availability.""" - time.sleep(1) return random.choice([True, False, False, False]) @@ -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: diff --git a/python-while-loop/for_loop.py b/python-while-loop/for_loop.py new file mode 100644 index 0000000000..ea022560b2 --- /dev/null +++ b/python-while-loop/for_loop.py @@ -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}") diff --git a/python-while-loop/logfile.py b/python-while-loop/logfile.py index 3f59766c20..709f5f98ad 100644 --- a/python-while-loop/logfile.py +++ b/python-while-loop/logfile.py @@ -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) diff --git a/python-while-loop/password.py b/python-while-loop/password.py index cabc8b7ef7..c056ce136b 100644 --- a/python-while-loop/password.py +++ b/python-while-loop/password.py @@ -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.") diff --git a/python-while-loop/process_pairs.py b/python-while-loop/process_pairs.py new file mode 100644 index 0000000000..4b278dc35c --- /dev/null +++ b/python-while-loop/process_pairs.py @@ -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}") diff --git a/python-while-loop/temperature.py b/python-while-loop/temperature.py index e8725ce2e4..b07a629d7b 100644 --- a/python-while-loop/temperature.py +++ b/python-while-loop/temperature.py @@ -3,7 +3,6 @@ def read_temperature(): - time.sleep(1) return random.uniform(20.0, 30.0) @@ -11,6 +10,8 @@ def read_temperature(): 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)