diff --git a/python-while-loop/break.py b/python-while-loop/break.py new file mode 100644 index 0000000000..23aa933a3a --- /dev/null +++ b/python-while-loop/break.py @@ -0,0 +1,9 @@ +number = 6 + +while number > 0: + number -= 1 + if number == 2: + break + print(number) + +print("Loop ended") diff --git a/python-while-loop/connection.py b/python-while-loop/connection.py index 859f1c03e5..cb90ef8fc5 100644 --- a/python-while-loop/connection.py +++ b/python-while-loop/connection.py @@ -8,7 +8,7 @@ attempts += 1 print(f"Attempt {attempts}: Connecting to the server...") # Simulating a connection scenario - time.sleep(0.5) + time.sleep(0.3) if random.choice([False, False, False, True]): print("Connection successful!") break diff --git a/python-while-loop/continue.py b/python-while-loop/continue.py new file mode 100644 index 0000000000..4cc564aeac --- /dev/null +++ b/python-while-loop/continue.py @@ -0,0 +1,9 @@ +number = 6 + +while number > 0: + number -= 1 + if number == 2: + continue + print(number) + +print("Loop ended") diff --git a/python-while-loop/for_loop.py b/python-while-loop/for_loop.py index ea022560b2..ae0ea6bb5a 100644 --- a/python-while-loop/for_loop.py +++ b/python-while-loop/for_loop.py @@ -1,11 +1,11 @@ requests = ["first request", "second request", "third request"] -print("\nWith a for-loop") +print("\nWith a for loop") for request in requests: print(f"Handling {request}") -print("\nWith a while-loop") +print("\nWith a while loop") it = iter(requests) while True: try: diff --git a/python-while-loop/guess.py b/python-while-loop/guess.py new file mode 100644 index 0000000000..38494e2bde --- /dev/null +++ b/python-while-loop/guess.py @@ -0,0 +1,19 @@ +from random import randint + +LOW, HIGH = 1, 10 + +secret_number = randint(LOW, HIGH) +clue = "" + +# Game loop +while True: + guess = input(f"Guess a number between {LOW} and {HIGH} {clue} ") + number = int(guess) + if number > secret_number: + clue = f"(less than {number})" + elif number < secret_number: + clue = f"(greater than {number})" + else: + break + +print(f"You guessed it! The secret number is {number}")