From bf2b3fde84224443ea05e0fc64c16f06d25494fe Mon Sep 17 00:00:00 2001 From: Chris Mekelburg Date: Sat, 7 Sep 2024 21:23:49 -0400 Subject: [PATCH] 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. --- ranges.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ranges.py b/ranges.py index 2fa00d7..7910069 100644 --- a/ranges.py +++ b/ranges.py @@ -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}")