ck 2 updated range.py

I found it interesting that the range function can be used to avoid the
need for a long list in python. I also found it interesting that the
range does not use the last item (the maximum), so I added maximum+1 in
a couple of spots to make the output read more accurately. I was
surprised that it worked that easily, but am also wondering if there is
a way to use a range as inclusive of both the start and end value.
Another thing I am curious about is how to use ranges for a purpose
besides generating a list of numbers.
This commit is contained in:
Chris Mekelburg 2024-09-07 21:23:49 -04:00
parent 29b250ff83
commit bf2b3fde84
1 changed files with 7 additions and 4 deletions

View File

@ -4,20 +4,23 @@
def print_all_numbers(maximum):
"Prints all integers from 0 to maximum."
for number in range(maximum):
for number in range(maximum+1):
print(number)
def print_even_numbers(maximum):
"Prints all even integers from 0 to maximum."
pass
for number in range(0,maximum+1,2):
print(number)
def print_odd_numbers(maximum):
"Prints all odd integers from 0 to maximum."
pass
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."
pass
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}")