@@ -195,10 +195,77 @@ These are answers for exercises in the chapters. In programming, there's always
195195 print (" Wrong username or password." )
196196 ```
197197
198+ # # Trey Hunner: zip and enumerate
199+
200+ 1 . Read some lines with `input ` and then enumerate it.
201+
202+ ```py
203+ print (" Enter something, and press Enter without typing anything" ,
204+ " when you're done." )
205+
206+ lines = []
207+ while True :
208+ line = input (' >' )
209+ if line == ' ' :
210+ break
211+ lines.append(line)
212+
213+ for index, line in enumerate (lines, start = 1 ):
214+ print (" Line" , index, " is:" , line)
215+ ```
216+
217+ 2 . Let' s start by trying out `zip` with strings:
218+
219+ ```py
220+ >> > for pair in zip (' ABC' , ' abc' ):
221+ ... print (pair)
222+ ...
223+ (' A' , ' a' )
224+ (' B' , ' b' )
225+ (' C' , ' c' )
226+ >> >
227+ ```
228+
229+ Great, it works just like it works with lists. Now let' s create
230+ the letter printing program:
231+
232+ ```py
233+ uppercase = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
234+ lowercase = ' abcdefghijklmnopqrstuvwxyz'
235+
236+ for upper, lower in zip (uppercase, lowercase):
237+ print (upper, lower)
238+ ```
239+
240+ 3 . This one is a bit more difficult than the other two because we
241+ need to combine `zip ` and `enumerate ` . I would pass a `zip `
242+ result to `enumerate ` , like this:
243+
244+ ```py
245+ uppercase = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
246+ lowercase = ' abcdefghijklmnopqrstuvwxyz'
247+
248+ for index, letterpair in enumerate (zip (uppercase, lowercase), start = 1 ):
249+ upper, lower = letterpair
250+ print (index, upper, lower)
251+ ```
252+
253+ Another alternative is to pass an `enumerate ` result to `zip ` . This is
254+ a bit more complicated, so I wouldn' t do it this way.
255+
256+ ```py
257+ uppercase = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
258+ lowercase = ' abcdefghijklmnopqrstuvwxyz'
259+
260+ for upper, indexlowerpair in zip (uppercase, enumerate (lowercase, start = 1 )):
261+ index, lower = indexlowerpair
262+ print (index, upper, lower)
263+ ```
264+
198265# # Defining functions
199266
200- 1 . Use `- value` to get the negative in the abs function, and for loops
201- in the other two functions.
267+ 1 . Use `- value` (it works just like ` - 1 ` ) to get the negative in
268+ the abs function, and for loops in the other two functions.
202269
203270 ```py
204271 def my_abs (value ):
@@ -213,7 +280,7 @@ These are answers for exercises in the chapters. In programming, there's always
213280 for item in a_list:
214281 if item:
215282 return True # ends the function
216- return True
283+ return False
217284
218285 def my_all (a_list ):
219286 for item in a_list:
@@ -225,10 +292,10 @@ These are answers for exercises in the chapters. In programming, there's always
2252922 . Like this:
226293
227294 ```py
228- def box (message , character = ' *' ):
295+ def print_box (message , character = ' *' ):
229296 number_of_characters = len (message) + 4
230297 print (character * number_of_characters)
231- print (message)
298+ print (character, message, character )
232299 print (character * number_of_characters)
233300 ```
234301
0 commit comments