Files
lab_iteration/ranges.py
angelotr 474b5cf9f0 I implemented the missing functions by replacing the pass statements with for loops that use range and step sizes. This allows print_even_numbers, print_odd_numbers, and print_multiples_of_five correctly.
Checkpoint 2:

What I understood well was the print_even_numbers, print_odd_numbers, and print_multiples_of_five functions because it reminded me of an assignment I had in my previous coding courses.
What I found interesting about ranges is that it included the first and last numbers in the list. My initial assumption was that when using ranges it would call every number in between the first and last digit, excluding the first and last digit.
With ranges I am a little unsure about whether range includes the maximum numberor stops just before it, and when I should add the +1 to make sure the endpoint is included.
2025-09-14 14:11:46 -04:00

36 lines
1.1 KiB
Python

# ranges.py
# ---------
# By MWC Contributors
def print_all_numbers(maximum):
"Prints all integers from 0 to maximum."
for number in range(maximum + 1):
print(number)
def print_even_numbers(maximum):
"Prints all even integers from 0 to maximum."
for number in range(0, maximum + 1, 2):
print(number)
def print_odd_numbers(maximum):
"Prints all odd integers from 0 to maximum."
for number in range(1, maximum + 1, 2):
print(number)
def print_multiples_of_five(maximum):
"Prints all integers which are multiples of five from 0 to maximum."
for number in range(0, maximum + 1, 5):
print(number)
chosen_maximum = int(input("Choose a number: "))
print(f"All numbers from 0 to {chosen_maximum}")
print_all_numbers(chosen_maximum)
print(f"All even numbers from 0 to {chosen_maximum}")
print_even_numbers(chosen_maximum)
print(f"All odd numbers from 0 to {chosen_maximum}")
print_odd_numbers(chosen_maximum)
print(f"All multiples of 5 from 0 to {chosen_maximum}")
print_multiples_of_five(chosen_maximum)