Presentation is loading. Please wait.

Presentation is loading. Please wait.

The fourth programming assignment J.-F. Pâris Fall 2014.

Similar presentations


Presentation on theme: "The fourth programming assignment J.-F. Pâris Fall 2014."— Presentation transcript:

1 The fourth programming assignment J.-F. Pâris jfparis@uh.edu jfparis@uh.edu Fall 2014

2 What we want to achieve Maintain a day schedule of appointments  User is asked to enter appointments that each contains: A time in AM/PM format A brief description  Each time the user has done so, the program displays all his or her appointments in sorted order.

3 Storing appointments (I) Appointments could be represented by a list containing  The appointment time  A brief description Think of ['2:30PM', 'COSC 1306'] This will not be our final design but puts us on the right track

4 Storing the day schedule (I) The day schedule will then be represented as a list of appointments  Yes, it will be a list of lists! Think of [['2:30PM', 'COSC 1306'], ['5:30PM', COSC 6360']]

5 Keeping the list sorted We are asked to keep the list sorted  When sorting list of lists, Python default is to sort by the first element of each sublist So the list [[ 'John', 'Doe'], ['Jane', Doe]] will be sorted as [ ['Jane', Doe], [ 'John', 'Doe']]

6 The problem Assume we want to sort the schedule [['9:00AM', 'Meeting'], ['1:30PM', COSC 1306']] Python will sort it as [ ['1:30PM', COSC 1306'], ['9:00PM', 'Meeting']] because '1:30PM' < '9:00PM'

7 The solution Many solutions are possible including writing our own sorting function Simplest solution is to add military times of our appointments  We already have the code that does that!

8 Storing appointments (II) Appointments will be represented by a list containing  The appointment time in military time  The appointment time as it was entered  A brief description in that order Think of ['1430','2:30PM', 'COSC 1306']

9 Storing the day schedule (II) The day schedule will still be represented as a list of appointments Think of [[ '1430', '2:30PM', 'COSC 1306'], ['1730','5:30PM', COSC 6360']]

10 Program organization (I) Forever loop:  Prompt for a time  If user entered nothing, break  Prompt for a description  Compute military time  Add it to the appointment  Insert the new appointment into the schedule and sort it  Print the schedule

11 Notes We need to start with an empty schedule  schedule =[] We need to start afresh with an empty appointment at each iteration of the loop  apptmt = [ 0, 0] To test for the end of user input, test for an empty string Steps in italics must be implemented by functions that you will define  Before the beginning of your program!

12 Program organization (II) schedule= [] while True:  apptmt = [0, 0]  apptmt[0] = input("Enter ….: ")  if apptmt[0] = '' : break  apptmt.insert(0, militarytime(apptmt[0]))  schedule = update(schedule, apptmt)  display(schedule)

13 Notes Your program will be very short  Most of the work will be done in the three functions you will define  That's the way it should be Main program does not overwhelm the reader with all the details of the program

14 militarytime() function Its input parameter will be a time in AM/PM format Its result will be a military time So  militarytime('12:00AM') will return '0000'  military time('2:30PM') will return '1430' Just as in the third program

15 update() function (II) Python has two list methods that will do all the work for you:  lst.append()  lst.sort() In fact, your function will consist of two lines followed by a return!

16 display function Its input will be your schedule It will not return any result Will look like  for apptmt in schedule: print(" …" % (apptmt[1], apptmt[2]))

17 A last comment The assignment is not hard because most of the work has already be done in the third program To avoid problems, you have to pay attention to the differences among  the string 'alpha'  the list ['alpha'] that contains the string 'alpha'  the list [['alpha']] that contains the list ['alpha']


Download ppt "The fourth programming assignment J.-F. Pâris Fall 2014."

Similar presentations


Ads by Google