Home//

[Solved] Python for Everybody - Chapter 5: Iteration

[Solved] Python for Everybody - Chapter 5: Iteration

Minh Vu

By Minh Vu

Updated Nov 28, 2023

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:

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:

exercise-1.py
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:

Chapter 5: Iteration - Exercise 1
Figure: Chapter 5: Iteration - Exercise 1

Explanation:

  1. while True means the loop will run forever until we use break to stop it.
  2. We get the input from the user using input() function.
  3. Use try/except to handle invalid numbers, i.e. the user enters anything other than a number.
  4. If the user enters done, we use break to stop the loop.
  5. Otherwise, we add the number to total and increase count by 1.
  6. 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:

exercise-2.py
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:

Chapter 5: Iteration - Exercise 2
Figure: Chapter 5: Iteration - Exercise 2

Explanation:

  1. while True means the loop will run forever until we use break to stop it.
  2. We get the input from the user using input() function.
  3. Use try/except to handle invalid numbers, i.e. the user enters anything other than a number.
  4. If the user enters done, we use break to stop the loop.
  5. Otherwise, we add the number to total and increase count by 1, then we update the maximum and minimum values.
  6. 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.

You can search for other posts at home page.
Minh Vu

Minh Vu

Software Engineer

Hi guys, I'm the author of WiseCode Blog. I mainly work with the Elastic Stack and build AI & Python projects. I also love writing technical articles, hope you guys have good experience reading my blog!