{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ecs10-11-26

ecs10-11-26 - Announcements ECS 10 11/15 Next assignment is...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 11/26/2007 Announcements ECS 10 11/15 Next assignment is on Web. Due Wds, Dec 5. No extensions! Wds, Uses a dictionary, which we will not discuss in class until Wd . Y can get started on other l il Wds You Wds. d h parts of the program though. Start reading Chapter 5 on dictionaries. Database A database is a program that answers questions about some data, lets you change it in specific ways. New Concepts How to change a global variable in a function How to stop a for loop early Both of these are ways of avoiding Python's biases about h to d things. bi b how do hi Python's biases are there to keep you organized, so use these new ideas rarely and carefully. You Y organize the d diff i h data differently depending on l d di the kinds of questions you have to answer. answer. On our CA congressional data, two kinds of queries: Who won in district 14? What district does Nancy Pelosi represent? Structure of Database Program Two parts: 1. Finding Names First job reading data, building database. Extract name, number of votes. Keep track of which one got the most votes. Store inn rs St r winners in a list of lists: [district, name] f lists [distri t name] n m 2. 2 Read input file and store relevant data in some kind of data structure (eg, list of lists) (eg, Have a loop which asks for queries and answers them using the data structure. We'll start with Part 1, same for both kinds of query. Extract names and votes from each line, pick winner in each district, and store winners in a list of lists -- [district, name] Are the candidates with stars the winners? 1 11/26/2007 ReDo the getVotes() function getVotes() Extract names instead of parties. Name might be two or three words Get rid of those stars! stars! Return a list [name, votes] This is all review. If you really need to review, don't look at it and try and write your own version. Find Winner - Warm Up Trick question: Does this program crash when max has value None? None? No! When max == None is True, then max < True, num never gets evaluated. Find Winner Just work on one district first. Break down problem into smaller pieces! Similar to finding maximum in list. Store winner i a li ( h "d B ") as a S i in list (the "dataBase "d dataBase") record which is itself a list [district, name]. Every Congressional District When we start a new district Store the winner of the current district in the data base. Reset winner to None. None But there are 53 districts...? What happened to the last one? Need to repeat code to store the winner. Make a function to avoid repeating code. Woops! We want to modify a global variable... Changing a Global Variable global dataBase asks permission to change a global variable in the function. Like exceptions, use it carefully, only when you know what you are doing. You usually don't have permission because it is really easy to make a big mess. 2 11/26/2007 Easy Queries Who is the representative in District 4? District is index into list Detail: list items are numbered 0,1,2,...52, but 0,1,2,...52, districts di i are numbered 1 2 3 53 So really b d 1,2,3...53. S ll district 1 is index of the record for that district . More difficult queries What if we want to answer: Select a representative: Pelosi Nancy Pelosi is the Representative of District 8 Read through all districts until we find the string "Pelosi" Read list, look for name Use another for loop. Check for last name match. What if the name is not found? Ending Early After we found the last name, why read the rest of the list? Doesn't hurt, but wastes time. Use the break statement to end loop early Break Immediately jumps out of the loop. Usually used with for loops. Avoid with while. Practice and Study Try writing the program on the previous slide using a while loop with no break statement. Try writing the version of the getVotes function that returns the name and number of votes. votes Start reading pages 147-154 so you understand 147Wds's lecture better. 3 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online