This tutorial gives you the solutions to the exercises in Chapter 5 of the book Python for Everybody: Exploring Data Using Python 3 by Charles R. Severance.
Link to the book:
Solutions for other chapters:
- [Solved] Python for Everybody - Chapter 5: Iteration (this tutorial)
- [Solved] Python for Everybody - Chapter 6: Strings
- [Solved] Python for Everybody - Chapter 8: Lists
Table of Contents
1. Chapter 5: Iteration - Exercise 1
Exercise 1: Write a program which repeatedly reads numbers until the user enters “done”. Once “done” is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try
and except
and print an error message and skip to the next number.
Answer:
total = 0 count = 0 average = 0 while True: number = input("Enter a number: ") try: number = float(number) total += number count += 1 except: if number == "done": break else: print("Invalid input") continue average = total / count print(total, count, average)
Output:

Explanation:
while True
means the loop will run forever until we usebreak
to stop it.- We get the input from the user using
input()
function. - Use
try/except
to handle invalid numbers, i.e. the user enters anything other than a number. - If the user enters
done
, we usebreak
to stop the loop. - Otherwise, we add the number to
total
and increasecount
by 1. - Finally, we calculate the average and print out the result.
2. Chapter 5: Iteration - Exercise 2
Exercise 2: Write another program that prompts for a list of numbers as above and at the end prints out both the maximum and minimum of the numbers instead of the average.
Answer:
total = 0 count = 0 maximum = None minimum = None while True: number = input("Enter a number: ") try: number = float(number) if maximum is None or number > maximum: maximum = number if minimum is None or number < minimum: minimum = number total += number count += 1 except: if number == "done": break else: print("Invalid input") continue print(total, count, maximum, minimum)
Output:

Explanation:
while True
means the loop will run forever until we usebreak
to stop it.- We get the input from the user using
input()
function. - Use
try/except
to handle invalid numbers, i.e. the user enters anything other than a number. - If the user enters
done
, we usebreak
to stop the loop. - Otherwise, we add the number to
total
and increasecount
by 1, then we update themaximum
andminimum
values. - Finally, we calculate the average and print out the result.
Conclusion
In this tutorial, we have solved 2 problems of the Chapter 5: Iteration in the book Python for Everybody: Exploring Data Using Python 3.
Please leave a comment if you have any questions.