Chapter 5 Loops Exercises

by Elliott Hauser

04 Feb 2016

Submit a well-formatted pull request to our class blog with embedded Trinket programs for the below two exercises from Chapter 5 (use these instead of the ones in the book - I added a few explanations). Complete these on your own, using only the materials in this Chapter. Do not look at other students’ submissions until after you’ve completed your work.

After your programs are done, check other students’ work and other resources online if you had questions. Include a reflection about what you think you’ve learned and any concepts that are still fuzzy to you. Did you encounter frustrating situations? Did you feel a lightbulb turn on?


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.

Here’s a sample run of the program:

Enter a number: 4
Enter a number: 5
Enter a number: bad data
Invalid input
Enter a number: 7
Enter a number: done
16 3 5.333333333333333

Exercise 2: Write another program that prompts for a series of numbers as above and at the end prints out the maximum, minimum and average of the numbers.

Note: this is slightly different than the textbook exercise

Elliott Hauser is a PhD Student in information science at UNC Chapel Hill. He's hacking education as one of the cofounders of Trinket.io. Find Elliott Hauser on Twitter, Github, and on the web.