Exercise: While loops

Practice while loops in these exercises, and run the doctests when you think you've got your code working. Remember, it's very easy (and common, even for experienced engineers) to code off-by-one errors, so keep your eyes peeled for those. 👀 Also, if you accidentally write an infinite loop, you will very likely crash the browser tab. If that happens, congratulations, that's a programmer rite of passage! Just reload the page and try to write a finite loop instead.

Count Evens

Implement count_evens, a function that counts the number of even numbers between a given start number and a given end number. The count should include the start or end numbers if they are also even.

Tip: To check if a number is even, use the % operator, which reports the remainder after dividing one number by another number. An even number divided by 2 has a remainder of 0.

📺 You can watch me walk through this problem in a video. If you don't want to see the solution, make sure to pause when you feel like you've seen enough to get started.

🧩 Or, for more guidance in written form, try a Parsons Puzzle version first.

def count_evens(start, end): """ Counts the number of even numbers between a given start number and given end number. Count should include the start or end if they are even. >>> count_evens(2, 2) 1 >>> count_evens(-2, 52) 28 >>> count_evens(237, 500) 132 """ # YOUR CODE HERE

Count Multiples

Implement count_multiples, a function that counts how many multiples of a given divisor are between the start and end numbers. It should include the start or end if they are also a multiple of divisor.

Tip: The % operator is once again helpful here, for determining if one number is a multiple of another. In fact, you may want to start from your previous code and see how little you need to change to get this function implemented correctly.

🧩 For more, try a Parsons Puzzle version first.

def count_multiples(start, end, divisor): """ Counts the number of multiples of divisor between the start and end numbers. It should include the start or end if they are a multiple. >>> count_multiples(2, 2, 1) # 2 is a multiple of 1 1 >>> count_multiples(2, 2, 2) # 2 is a multiple of 2 1 >>> count_multiples(2, 2, 3) # 2 is not a multiple of 3 0 >>> count_multiples(1, 12, 3) # 3, 6, 9, 12 4 >>> count_multiples(237, 500, 10) 27 """ # YOUR CODE HERE

Sum Multiples

Implement sum_multiples, a function that returns the sum of the multiples of a given divisor between the given start and end numbers. If the start or end numbers are also multiples, they should also be included in the sum.

Hint: The main difference between this exercise and the previous exercise is summing versus counting. What do you need to change from your previous code to make the return value a sum instead?

def sum_multiples(start, end, divisor): """Returns the sum of the multiples of a given divisor between start and end. If the start or end numbers are multiples, they should be included in the sum. >>> sum_multiples(1, 12, 4) 24 >>> sum_multiples(1, 12, 13) 0 >>> sum_multiples(2, 2, 2) 2 >>> sum_multiples(2, 2, 3) 0 >>> sum_multiples(23, 81, 13) 260 """ # YOUR CODE HERE

Debug this! 🐞🐛🐜

The code for the next function is already written, but it has several bugs.

Tips for debugging:

def product_of_numbers(end): """Returns the product of all the numbers from 1 to the end number, including the end number. >>> product_of_numbers(1) 1 >>> product_of_numbers(2) 2 >>> product_of_numbers(3) 6 >>> product_of_numbers(10) 3628800 """ result = 1 counter = 0 while counter < end: result *= counter counter += 2 return result
➡️ Next up: For Loops & Ranges