Skip to content
Open
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
9 changes: 9 additions & 0 deletions python-while-loop/break.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
number = 6

while number > 0:
number -= 1
if number == 2:
break
print(number)

print("Loop ended")
2 changes: 1 addition & 1 deletion python-while-loop/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions python-while-loop/continue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
number = 6

while number > 0:
number -= 1
if number == 2:
continue
print(number)

print("Loop ended")
4 changes: 2 additions & 2 deletions python-while-loop/for_loop.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
19 changes: 19 additions & 0 deletions python-while-loop/guess.py
Original file line number Diff line number Diff line change
@@ -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}")
Loading