''' menu.py Jeff Ondich, 9 April 2009 This program presents the user with a menu of options. After the person types a response, the program calls a function to perform the task the person selected. This is a small illustration of how to break a big problem (present a menu and act according to the user's selection) into a collection of smaller ones (print the menu, do each operation, etc.) using functions. ''' import random def print_menu(): print() print('A. Tell me a joke') print('B. Sing me a song') print('C. Give me a randomly chosen letter between A and Z') print() def get_joke(): joke = 'Why is 6 afraid of 7?\nBecause seven ate nine.' return joke def get_song(): song = 'Oh Danny boy, the pipes the pipes are calling\n' song += 'From glen to glen, and down the mountainside...' return song def get_random_letter(): random_integer = random.randrange(26) random_letter = chr(ord('A') + random_integer) return random_letter def do_operation(selection): if selection == 'A': joke = get_joke() print() print(joke) print() elif selection == 'B': song = get_song() print() print(song) print() elif selection == 'C': print('Here you go:', get_random_letter()) print() else: print('Please choose A, B, or C next time') print() # The main program begins here. print_menu() response = input('Your choice? ') do_operation(response)