Sum of numbers

Reads a number and adds up all numbers from 1 to it. Teaches input, variables and the for loop.

Open in Tokbit Level: Beginner. Free in the browser, nothing to install, no account needed.

Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.

Flowchart

Flowchart of the Sum of numbers example

Python program

The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.

"""Adds the numbers from 1 to n. Teaches a loop and a running total."""
n: int
n = int(input("Up to which number?"))
total: int = 0
# n + 1, because the range excludes its upper bound.
for i in range(1, n + 1):
    # Each step adds the next number to the total.
    total = total + i
print("The sum of numbers 1 to", n, "is", total)

Follow-up tasks

Tasks in which you change the example (with a model solution):

All examples