Coding challenges on control flow: branching and loops
Problem 1The for Loop and range()
Sum a Range of Integers
Assume you are given an integer variable named n (you may assume n is at least 1). Create a variable named total that holds the sum of all integers from 0 up to and including n - 1 — that is, 0 + 1 + 2 + ... + (n - 1).
Use a for loop together with range.
Scratchpad— run any Python to test ideas
Python runtime ready in a moment
Python runtime ready in a moment
Visible tests · 2Examples — click to expand
total is an integer
total equals 45
Hidden tests · 3Bodies hidden — pass/fail only
Hidden test 1
Hidden test 2
Hidden test 3