""" What is the purpose of this demo? As a student in a big university city, finding housing is quite difficult. Instead of painstakingly editing a standard message for reacting to an advertisement, you can use Python to fill in a template. You can do this using only for-loops and basic string manipulation. However, how you apply your for-loops has a big effect on the readability of your code. Below you can see a basic reaction template, feel free to customize it. The goal is to replace all placeholders [[...]] with their relevant values. But be careful, you need to match the right names to the right addresses and prices. Special care: [[NAME]] We want to be polite, so names should be nicely formatted. Between each pair of names should be a comma: Adam, Brigitte. But between the second to last and the last name on the list, use an ampersand instead: Brigitte & Carlos. """ REACTION_TEMPLATE = [ "Hi [[NAME]],", "", "Saw your listing for the room at [[ADDRESS]] and wanted to reach out.", "I'm a student in Delft looking for something close to campus, and [[PRICE]]", "a month is right around what I've budgeted, so this one caught my eye.", "", "I'm tidy, quiet during exam periods, and free most days if a viewing is", "still possible. Let me know!", "", "Cheers,", "[[YOUR NAME]]", ] # Each listing can have more than one current housemate to greet, so NAMES # is a list of lists. NAMES, ADDRESSES, and PRICES must stay the same length # and stay lined up: index i in every list describes the same listing. NAMES = [ ["Adam", "Brigitte", "Carlos"], # <- Adam, Brigitte & Carlos form one household ["Dennis"], ["Elif", "Fenna"], ["Gijs"], ["Hannah", "Ivo", "Julia"], ] ADDRESSES = [ "Oude Delft 133", # <- Adam, Brigitte & Carlos live here "Phoenixstraat 48", "Voorstraat 22", "Buitenwatersloot 90", "Nieuwe Langendijk 15", ] PRICES = [650, 525, 590, 475, 610] """ === Filling the template === """ """ --- Task 1: Make a plan --- This is a problem that is easier to tackle if you first make a plan due to the number of steps that have to occur in the right order. If available, draw a flowchart with pen and paper that shows the steps you need to take to replace all placeholders [[...]] with the correct values. Alternatively, answer the following five questions in a sentence or two: 1. In what datatype will you store the filled-in reactions? > 2. Over which list will you iterate for the outer-most loop? > 3. How can you construct the right format for the [[NAME]] placeholder? > 4. Is there a [[...]] placeholder that does not change between iterations? > 5. How can you check if your code works? > """ """ --- Task 2: Write the script --- (a) Execute the plan you have formulated in task 1 until the first time you need to replace one of the [[...]] placeholders. (b) Implement the replacement with the .replace() String method. (c) Execute the rest of your plan. Hint: You need to make sure that the right element from NAMES is associated with the right element from ADDRESSES and PRICES. You can do this by iterating over all indices. (d) If you got stuck, check the solution on the next page and answer the following questions: 1. Why did you get stuck? > 2. How did the sample solution tackle the issue? > """ """ === Refactoring with zip() and enumerate() === """ """ --- Issues in the example above --- 1. The range(len(ADDRESSES)) syntax is hard to read. 2. Trying to access an index outside of a list is a common source of bugs. 3. There are four levels of indentation and three running variables i, j & k that are easy to confuse. """ """ --- Task 1: Iterating over elements instead of indices --- (a) Refactor the iteration over lines in the REACTION_TEMPLATE to iterate over elements instead of indices. """ """ --- Task 2: The enumerate() function pairs element with index --- (a) Refactor the code to use enumerate() in the for-loop constructing names_connected. The index can help you tell whether to put a comma or an ampersand between individual names. """ """ --- Task 3: The zip() function groups elements of different lists --- (a) Refactor the outer for-loop to use zip() to iterate over advertisements. Supply all relevant lists to zip() and unpack the output into appropriately named variables. """ """ --- Task 4: Easy readability wins --- (a) Move the name-construction loop out of the inner loop. """