While Loop Implementation
print("-" * 10 + "Class Attendance Check" + "-" * 10)
response = input("Do you have class today? y/n") # Initialize variable
while response == "y": # Condition check
print("Class is required")
response = input("Do you have class today? y/n") # Update variable
print("-" * 10 + "Sum from 1 to 100" + "-" * 10)
total = 0 # Store cumulative sum
counter = 1 # Initialize counter
while counter <= 100: # Condition check
total += counter
counter += 1 # Update counter
print(total)
User Authentication Simulation with While Loop
attempts = 0 # Initialize attempt counter
while attempts < 3: # Check maximum attempts
# Get user credentials
username = input("Enter your username:")
password = input("Enter your password:")
# Authentication process
if username == 'ty' and password == '666666':
print('Logging in...')
attempts = 4 # Exit loop on success
else:
if attempts <= 2:
print('Invalid credentials. You have', 2 - attempts, 'attempts remaining')
attempts += 1 # Increment attempt counter
# Single branch condition
if attempts == 3:
print("All three login attempts failed")
Nested Loops for Pattern Pritning
print('-' * 10 + 'Print 3x4 Star Matrix' + '-' * 10)
for row in range(1, 4):
for col in range(1, 5):
print('*', end='') # Prevent line break
print() # Move to next line
print('-' * 10 + 'Print 5-row Right Triangle' + '-' * 10)
for row in range(1, 6):
for col in range(1, row + 1):
print('*', end='')
print()
print('-' * 10 + 'Print Inverted 5-row Right Triangle' + '-' * 10)
for row in range(1, 6):
for col in range(1, 7 - row):
print('*', end='')
print()
print('-' * 10 + 'Print 5-row Isosceles Triangle' + '-' * 10)
for row in range(1, 6):
for space in range(1, 6 - row):
print(' ', end='')
for star in range(1, row * 2):
print('*', end='')
print()
Diamond Pattern with Nested Loops
rows = int(input('Enter number of rows for diamond:'))
while rows % 2 == 0:
print('Please enter an odd number!')
rows = int(input('Enter number of rows for diamond:'))
upper_half = (rows + 1) // 2
# Upper part of diamond
for i in range(1, upper_half + 1):
for j in range(1, upper_half + 1 - i):
print(' ', end='')
for k in range(1, i * 2):
print('*', end='')
print()
# Lower part of diamond
lower_half = rows // 2
for i in range(1, lower_half + 1):
for j in range(1, i + 1):
print(' ', end='')
for k in range(1, 2 * lower_half - i * 2 + 2):
print('*', end='')
print()
Holllow Diamond Pattern
print('Hollow Diamond Pattern')
rows = int(input('Enter number of rows:'))
while rows % 2 == 0:
print('Please enter an odd number!')
rows = int(input('Enter number of rows:'))
upper_half = (rows + 1) // 2
# Upper part
for i in range(1, upper_half + 1):
for j in range(1, upper_half + 1 - i):
print(' ', end='')
for k in range(1, i * 2):
if k == 1 or k == i * 2 - 1:
print('*', end='')
else:
print(' ', end='')
print()
# Lower part
lower_half = rows // 2
for i in range(1, lower_half + 1):
for j in range(1, i + 1):
print(' ', end='')
for k in range(1, 2 * lower_half - i * 2 + 2):
if k == 1 or k == 2 * lower_half - i * 2 + 2 - 1:
print('*', end='')
else:
print(' ', end='')
print()
Break and Cotninue Statements
# Break example
sum_value = 0
index = 0
while index < 20:
sum_value += index
if sum_value > 100:
print('First number making sum exceed 100 is', index)
break
index += 1
# Break in for loop
sum_value = 0
for num in range(1, 20):
sum_value += num
if sum_value > 100:
break
print('First number making sum exceed 100 is', num)
# Continue example
index = 0
sum_value = 0
while index < 100:
if index % 2 == 1: # Odd numbers
index += 1
continue # Skip rest of loop body
sum_value += index
index += 1