- Recursion Hack
- Iterating over a List Hack
- Iterating over a list using loops and printing a message HACK
- Breaking Loops HACK
Recursion Hack
def factorial(n):
# Check for edge cases
if not isinstance(n, int) or n < 0:
return "Error: Factorial is only defined for non-negative integers."
elif n == 0 or n == 1: # Base cases
return 1
else:
return n * factorial(n - 1) # Recursive call
# Input for a number between 9 and 13
number = 11
result = factorial(number)
# Display the result with a custom message
if isinstance(result, int):
print(f"The factorial of {number} is {result}.")
else:
print(result)
Iterating over a List Hack
# Define the list of numbers
numbers = [1, 2, 3, 4, 5]
# Iterate through the list and print each number
for number in numbers:
print(number)
1
2
3
4
5
Iterating over a list using loops and printing a message HACK
# List of colors
colors = ["red", "blue", "green", "yellow"]
# Iterate through the list and print each color
for color in colors:
print(color)
red
blue
green
yellow
Breaking Loops HACK
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Iterate through the list
for number in numbers:
if number == 3:
continue # Skip the number 3
elif number == 5:
break # Stop the loop when 5 is encountered
print(number)
1
2
4