From 6c4e660819c4dd1f7f8c92caa134aabcafbba08d Mon Sep 17 00:00:00 2001 From: Junior Date: Mon, 3 Jun 2024 19:15:02 -0400 Subject: [PATCH] Some simple counters and totals --- Python/016_counters.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Python/016_counters.py diff --git a/Python/016_counters.py b/Python/016_counters.py new file mode 100644 index 0000000..06cf15f --- /dev/null +++ b/Python/016_counters.py @@ -0,0 +1,28 @@ +# A simple example of counters, subtotals, and totals + +# A simple loop that counts to 10 +loop_counter = 1 +print("Let's count to 10!") +print(" ", end='') +while loop_counter <= 10: + print(f" {loop_counter}", end='') + loop_counter += 1 +print() + +# Let's add up all the even numbers lower than 100 +even_total = 0 +span = range(0, 101, 2) +for num in span: + even_total += num +print(f"The sum of all even numbers between 0 and 100 is {even_total}") + +# Let's add up all the even numbers lower than 100 +odd_total = 0 +span = range(1, 101, 2) +for num in span: + odd_total += num +print(f"The sum of all odd numbers between 0 and 100 is {odd_total}") + +# Now we total them all up +total = odd_total + even_total +print(f"The sum of all numbers between 0 and 100 is {total}") \ No newline at end of file