7e. Review: Loops with Decisions#

More practice after Chapter 7: Iteration and Search. These questions practise the pattern at the heart of this page: a for loop with a decision inside it. Sometimes the decision is one if; sometimes it is a chain of elifs; sometimes it is an if inside another if. In every question, work out which branch a value takes before you write the code.

Try each question first, then open its Answer.

Run this page on Colab

Practice questions#

Question 1 (easy): write a function#

Write a function called count_evens that takes n and returns how many even numbers are in range(n), starting at 0.

For example, count_evens(10) is 5 (0, 2, 4, 6, 8). count_evens(1) is 1 (just 0 — is 0 even? Yes, 0 % 2 is 0).

Start from this header (replace ... with the body):

def count_evens(n):
    ...

Question 2 (easy): write a function#

Write a function called sum_evens that takes n and returns the sum of the even numbers from 0 through n.

For example, sum_evens(10) is 30 (0 + 2 + 4 + 6 + 8 + 10).

Start from this header (replace ... with the body):

def sum_evens(n):
    ...

Question 3 (medium): write a function#

Write a function called fizzbuzz that prints, one per line, for 1 through n: 'FizzBuzz' for a number divisible by both 3 and 5, 'Fizz' for a number divisible by 3 only, 'Buzz' for a number divisible by 5 only, and the number itself otherwise.

For example, fizzbuzz(15) ends with ..., 13, 14, FizzBuzz (each on its own line).

Question to think about first: what goes wrong if you test divisibility by 3 before you test divisibility by both?

Start from this header (replace ... with the body):

def fizzbuzz(n):
    ...

Question 4 (easy): write a function#

Write a function called count_vowels that takes a word and returns how many letters in the word are vowels.

For example, count_vowels('Hello World') is 3.

Start from this header (replace ... with the body):

def count_vowels(word):
    ...

Question 5 (medium): write a function#

Write a function called describe_number that takes n and returns 'positive even', 'positive odd', 'negative even', 'negative odd', or 'zero'.

For example, describe_number(4) is 'positive even'; describe_number(-3) is 'negative odd'; describe_number(0) is 'zero'.

Start from this header (replace ... with the body):

def describe_number(n):
    ...

Question 6 (medium): write a function#

Write a function called count_even_and_div3 that takes n and returns how many numbers from 0 through n are even AND divisible by 3.

For example, count_even_and_div3(20) is 4 (0, 6, 12, 18).

Start from this header (replace ... with the body):

def count_even_and_div3(n):
    ...

Question 7 (medium): write a function#

Write a function called classify_letters that takes a string s and prints, each on its own line, how many vowels, how many consonants, and how many other characters are in s.

For example, classify_letters('Hi! 123') prints:

1
1
5

Start from this header (replace ... with the body):

def classify_letters(s):
    ...

Question 8 (hard): write a function#

Write a function called first_even_over that takes n and limit and returns the first even number from n through limit, or None if there is none.

For example, first_even_over(7, 20) is 8; first_even_over(7, 7) is None.

Start from this header (replace ... with the body):

def first_even_over(n, limit):
    ...

Credits#

More review questions by Prof. Y. Rosenthal, for Think Python: 3rd Edition by Allen B. Downey. Text license: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International