diff --git a/python-while-loop/README.md b/python-while-loop/README.md new file mode 100644 index 0000000000..495bc87bfa --- /dev/null +++ b/python-while-loop/README.md @@ -0,0 +1,3 @@ +# Python while Loops: Repeating Tasks Conditionally + +This folder provides the code examples for the Real Python tutorial [Python while Loops: Repeating Tasks Conditionally](https://realpython.com/python-while-loop/). diff --git a/python-while-loop/api_calls.py b/python-while-loop/api_calls.py new file mode 100644 index 0000000000..bc3747e038 --- /dev/null +++ b/python-while-loop/api_calls.py @@ -0,0 +1,29 @@ +import random +import time + + +def is_api_available(): + """Simulate API availability.""" + time.sleep(1) + return random.choice([True, False, False, False]) + + +def make_api_call(request): + print(f"Making API call: {request}") + time.sleep(0.5) + + +requests = iter(["Request 1", "Request 2", "Request 3"]) +request = next(requests) + +while True: + if not is_api_available(): + print("API not available. Retrying in 1 sec...") + continue + make_api_call(request) + try: + request = next(requests) + except StopIteration: + break + +print("All requests processed.") diff --git a/python-while-loop/check_file.py b/python-while-loop/check_file.py new file mode 100644 index 0000000000..d1f3d85a6b --- /dev/null +++ b/python-while-loop/check_file.py @@ -0,0 +1,15 @@ +import time +from pathlib import Path + +filename = Path("hello.txt") + +print(f"Waiting for {filename.name} to be created...") + +while not filename.exists(): + print("File not found. Retrying in 1 second...") + time.sleep(1) + +print(f"{filename} found! Proceeding with processing.") +with open(filename, mode="r") as file: + print("File contents:") + print(file.read()) diff --git a/python-while-loop/connection.py b/python-while-loop/connection.py new file mode 100644 index 0000000000..859f1c03e5 --- /dev/null +++ b/python-while-loop/connection.py @@ -0,0 +1,18 @@ +import random +import time + +MAX_RETRIES = 5 +attempts = 0 + +while attempts < MAX_RETRIES: + attempts += 1 + print(f"Attempt {attempts}: Connecting to the server...") + # Simulating a connection scenario + time.sleep(0.5) + if random.choice([False, False, False, True]): + print("Connection successful!") + break + + print("Connection failed. Retrying...") +else: + print("All attempts failed. Unable to connect.") diff --git a/python-while-loop/infinite.py b/python-while-loop/infinite.py new file mode 100644 index 0000000000..b3066e789f --- /dev/null +++ b/python-while-loop/infinite.py @@ -0,0 +1,18 @@ +# number = 5 +# while number != 0: +# print(number) +# number -= 2 + + +number = 5 +while number > 0: + print(number) + number -= 2 + + +number = 5 +while number != 0: + if number <= 0: + break + print(number) + number -= 2 diff --git a/python-while-loop/logfile.py b/python-while-loop/logfile.py new file mode 100644 index 0000000000..3f59766c20 --- /dev/null +++ b/python-while-loop/logfile.py @@ -0,0 +1,16 @@ +import time + +with open("file.log", mode="r") as file: + file.seek(0, 2) + + while True: + line = file.readline() + if line: + line_content = line.strip() + if line_content == "END": + print("File processing done.") + break + print(f"Processing line content: {line_content}") + else: + time.sleep(1) + print("No new content. Retrying in 1 second...") diff --git a/python-while-loop/password.py b/python-while-loop/password.py new file mode 100644 index 0000000000..cabc8b7ef7 --- /dev/null +++ b/python-while-loop/password.py @@ -0,0 +1,19 @@ +MAX_ATTEMPTS = 3 + +correct_password = "secret123" +attempts = 0 + + +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 + else: + print(f"Incorrect password. {MAX_ATTEMPTS - attempts} attempts left.") diff --git a/python-while-loop/remove.py b/python-while-loop/remove.py new file mode 100644 index 0000000000..54483e9a7b --- /dev/null +++ b/python-while-loop/remove.py @@ -0,0 +1,5 @@ +colors = ["red", "blue", "yellow", "green"] + +while colors: + color = colors.pop(-1) + print(f"Processing color: {color}") diff --git a/python-while-loop/temperature.py b/python-while-loop/temperature.py new file mode 100644 index 0000000000..e8725ce2e4 --- /dev/null +++ b/python-while-loop/temperature.py @@ -0,0 +1,16 @@ +import random +import time + + +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: + print("Required temperature reached! Stopping monitoring.") + break diff --git a/python-while-loop/user_input.py b/python-while-loop/user_input.py new file mode 100644 index 0000000000..fcd4c2a814 --- /dev/null +++ b/python-while-loop/user_input.py @@ -0,0 +1,9 @@ +line = input("Type some text: ") + +while line != "stop": + print(line) + line = input("Type some text: ") + + +while (line := input("Type some text: ")) != "stop": + print(line)