bembry.org
Home / Technology / Python / Exercises

Python Lesson 13 Exercises: Avoiding Common Gotchas

In the following exercise, pay attention to your user interface, and be certain to test the programs rigorously. Programs that yield traceback errors will not be graded. Save each program as pa13-1.py, pa13-2.py and so forth.

Exercise 1:
Create a "change maker" program. The program will ask for the amount of change to be given a customer, then calculate how many of each monetary unit should be given (dollars, quarters, dimes, nickles, pennies). For example, if a customer is to receive 1.89 in change, the program should yeild the following:
dollars = 0
quarters = 3
dimes = 1
nickels = 0
pennies = 4

Exercise 2:
Write a program that will determine if every parenthesis in an expression has a matching partner. The program should allow the user to keep entering test cases until they want to stop, then print out all test results.

Input:
Expression (press enter to end): 12*(1+2)
Expression (press enter to end): (There (or here)but not over
Expression (press enter to end): (12*(123+16)-82(9)
Expression (press enter to end):
Output:
12*(1+2) : Matched
(There (or here)but not over : Unmatched
(12*(123+16)-82(9: Unmatched

Exercise 3:
Write a program that will print a diamond with a maximum width of the size entered by the user. The diamond should be centered on the screen. For example, if the user requests a diamond with a width of 5, the program will print:

   *
  ***
 *****
  ***
   *

Lesson Exercises Python Lessons Homepage

Restricted access