@@ -220,84 +220,33 @@ isn't exactly like mine but it works just fine it's ok, and you can
220220
221221# # Loops
222222
223- 1 . For loop over the users, each user is a list that contains a
224- username and a password.
223+ 1 . The problem is that `things` is a string because we converted it to a
224+ string with `str ` , so the for loop loops over the characters `[`,
225+ `1 ` , `,` and so on. Replace `str ([1 , 2 , 3 , 4 , 5 ])` with
226+ `[1 , 2 , 3 , 4 , 5 ]` .
225227
226- ```py
227- users = [
228- [' foo' , ' biz' ],
229- [' bar' , ' baz' ],
230- ]
231-
232- username = input (" Username: " )
233- password = input (" Password: " )
234-
235- logged_in = False
236- for user in users:
237- if username == user[0 ] and password == user[1 ]:
238- logged_in = True
239- break
240-
241- if logged_in:
242- print (" Welcome, " + username + " !" )
243- else :
244- print (" Wrong username or password." )
245- ```
246-
247- 2 . Just put the whole thing in a `while True :` . Remember that a break
248- will always break the innermost loop it' s in.
228+ 2 . The code appends each list in `before` to `after` , so the `number`
229+ variable actually pointed to a list like `[1 , 2 ]` . An easy solution
230+ is to just write two for loops inside each other:
249231
250232 ```py
251- users = [
252- [' foo' , ' biz' ],
253- [' bar' , ' baz' ],
254- ]
255-
256- while True :
257- username = input (" Username: " )
258- password = input (" Password: " )
259-
260- logged_in = False
261- for user in users:
262- if username == user[0 ] and password == user[1 ]:
263- logged_in = True
264- break # break the for loop
265-
266- if logged_in:
267- print (" Welcome, " + username + " !" )
268- break # break the while loop
269- else :
270- print (" Wrong username or password." )
233+ before = [[1 , 2 ], [3 , 4 ], [5 , 6 ]]
234+ after = []
235+ for sublist in before:
236+ for number in sublist:
237+ after.append(number)
238+ print (after)
271239 ```
272240
273- 3 . Add a for loop that works as an attempt counter.
241+ Lists also have an extend method that appends each item from another
242+ list , so we can also use that:
274243
275244 ```py
276- users = [
277- [' foo' , ' biz' ],
278- [' bar' , ' baz' ],
279- ]
280-
281- for attempts_left in [3 , 2 , 1 , 0 ]:
282- if attempts_left == 0 :
283- print (" No attempts left!" )
284- break # break the outer for loop
285-
286- print (attempts_left, " attempts left." )
287- username = input (" Username: " )
288- password = input (" Password: " )
289-
290- logged_in = False
291- for user in users:
292- if username == user[0 ] and password == user[1 ]:
293- logged_in = True
294- break # break the inner for loop
295-
296- if logged_in:
297- print (" Welcome, " + username + " !" )
298- break # break the outer for loop
299- else :
300- print (" Wrong username or password." )
245+ before = [[1 , 2 ], [3 , 4 ], [5 , 6 ]]
246+ after = []
247+ for sublist in before:
248+ after.extend(sublist)
249+ print (after)
301250 ```
302251
303252# # Trey Hunner: zip and enumerate
0 commit comments