1 / 7

Breaking the problem down

Breaking the problem down. In pairs, spend 5 minutes writing down broadly what you contact management program will do from the moment you run it. Brainstorm as a group. How can these steps be broken down into manageable parts?. A simple algorithm. Run program…

inez
Download Presentation

Breaking the problem down

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Breaking the problem down • In pairs, spend 5 minutes writing down broadly what you contact management program will do from the moment you run it. • Brainstorm as a group. • How can these steps be broken down into manageable parts?

  2. A simple algorithm • Run program… • Read contacts from file into list • Present user menu • While user doesn't quit • if they want to add a contact • add a contact • if they want to look up a contact • find the right contact • display the contact's details on the screen • Write the list into the file Pay particular attention to what is in the loop, and what is not in the loop…

  3. Functional Decomposition: What does my programming need to be able to do? • Read contacts from file • Display menu • Add a new contact • Search and display a contact's details • Write contacts to file • You will also need a main routine to knit these together

  4. Functions addContact Function defaddContact(contact): contactList.append(contact) searchContact Function defsearchContact(name): for contact in contactList: if contact.name = name: return contact

  5. File Structure John Smith, 0904 364 8687 Carla Singson, 0916 475 3499 Keith Yang, 0903 245 2457 Joseph Hadaway, 0906 234 6545 Comma-delimited

  6. Reading line-by-line Create a file called infile.txt and put a few lines of text in it. Then run this code and see what happens. fileIn= open('c:\infile.txt', 'r') s = fileIn.readline() count = 0 while s != "": count = count + 1 print (str(count), ' ', s) s = fileIn.readline() You need to understand and adapt this code. Not just copy it!

  7. Writing a list to a comma-delimited file fileOut= open('c:\data.txt', 'a') for obj in myList: fileOut.write(obj.fld1+','+obj.fld2) You need to understand and adapt this code. Not just copy it!

More Related