70 likes | 224 Views
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…
E N D
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… • 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…
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
Functions addContact Function defaddContact(contact): contactList.append(contact) searchContact Function defsearchContact(name): for contact in contactList: if contact.name = name: return contact
File Structure John Smith, 0904 364 8687 Carla Singson, 0916 475 3499 Keith Yang, 0903 245 2457 Joseph Hadaway, 0906 234 6545 Comma-delimited
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!
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!