BigSteve/Python/016_counters.py

28 lines
768 B
Python

# 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}")