Coding challenges on iteration in action: break, strings, and guess-and-check
Problem 1The break Statement: Exiting a Loop Early
Count Even Numbers in a Range
Assume you are given a positive integer variable named n. Write Python code that creates a variable named even_count holding the number of even integers in the sequence 0, 1, 2, ..., n - 1 (the values produced by range(n)). Zero counts as even.
For example, when n is 5, the values are 0, 1, 2, 3, 4, of which 0, 2, and 4 are even, so even_count should be 3.
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
even_count is an integer
range(5) has 3 even numbers
Hidden tests · 3Bodies hidden — pass/fail only
Hidden test 1
Hidden test 2
Hidden test 3