bembry.org
Home / Technology / Python / Exercises

Python Lesson 6 Exercises: Homemade Functions

In the following exercise, pay attention to your user interface, and be certain to test the codes to assure they work. Save each program as pa6-1.py, pa6-2.py and so forth.

Exercise 1:
Create a function called name() that will print out your name and grade when called. Then, create a short program that uses this function.

Exercise 2:
Create a function called title() that will take as input the name you wish to give a program and then return that name centered inside a box of stars. This function will serve as a method for automatically placing a nice little title at the top of programs.

 
  *******************************************
  *                                         *
  *            My Great Program             *
  *                                         *
  *******************************************

Exercise 3:
Create a dice-rolling function that will simulate rolling two dice. The function should print out what number (between 1 and 6) was rolled on each dice, print out that number of *s, and then tell the total of the roll. Create a program that calls this dice function three times. The program should also use the title() function created in exercise 2.
Sample Output:

  Dice 1: 2 **
  Dice 2: 6 ******
  ---------------------
  Total:  8

Exercise 4:
Create a program that will draw a square of whatever size the user requests. The main program should ask for the number of stars and send that value to a home-made function.
Sample:

  How big do you want your square?  5
  *****
  *****
  *****
  *****
  *****

Lesson Exercises Python Lessons Homepage

Restricted access