Skip to content

Commit 36b737f

Browse files
committed
loop stuff
1 parent dde8f28 commit 36b737f

File tree

2 files changed

+58
-88
lines changed

2 files changed

+58
-88
lines changed

basics/answers.md

Lines changed: 20 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -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

basics/loops.md

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,31 @@ with than while loops and index variables, especially in large projects.
238238
For looping is also a little bit faster than while looping with an index
239239
variable.
240240

241+
For loops are not actually limited to lists. We can for loop over many
242+
other things also, including strings and
243+
[tuples](lists-and-tuples.md#tuples). For looping over a tuple gives us
244+
its items, and for looping over a string gives us its characters as
245+
strings of length one.
246+
247+
```py
248+
>>> for short_string in 'abc':
249+
... print(short_string)
250+
...
251+
a
252+
b
253+
c
254+
>>> for item in (1, 2, 3):
255+
... print(item)
256+
...
257+
1
258+
2
259+
3
260+
>>>
261+
```
262+
263+
If we can for loop over something, then that something is **iterable**.
264+
Lists, tuples and strings are all iterable.
265+
241266
There's only one big limitation with for looping over lists. We
242267
shouldn't modify the list in the for loop. If we do, the results can
243268
be surprising:
@@ -275,10 +300,6 @@ Or if we just want to clear a list, we can use the `clear`
275300
>>>
276301
```
277302

278-
Later we'll learn that for loops are not actually limited to lists.
279-
There are many other things we can for loop over. Things that can be for
280-
looped over are called **iterables**.
281-
282303
If you're using Python 3.2 or older you need to use `stuff[:]` instead
283304
of `stuff.copy()` and `stuff[:] = []` instead of `stuff.clear()`.
284305
`stuff[:]` is a slice of the whole list, just like `stuff[0:]`.
@@ -403,25 +424,25 @@ while True:
403424

404425
## Exercises
405426

406-
1. Back in "Using if, else and elif" we created a program that asked
407-
for username and password and checks them, and we made users "foo"
408-
and "bar" with passwords "biz" and "baz". Adding a new user would
409-
have required adding more code that checks the username and
410-
password. Add this to the beginning of the program:
427+
1. This code is supposed to print each number between 1 and 5. Fix it.
411428

412429
```py
413-
users = [
414-
['foo', 'biz'],
415-
['bar', 'baz'],
416-
]
430+
things = str([1, 2, 3, 4, 5])
431+
for thing in things:
432+
print(thing)
417433
```
418434

419-
Then rewrite the rest of the program using a for loop.
435+
2. This code is supposed to print `[1, 2, 3, 4, 5, 6]`. Fix it.
420436

421-
2. Make the program ask the username and password over and over again
422-
until the user enters them correctly.
437+
```py
438+
before = [[1, 2], [3, 4], [5, 6]]
439+
after = []
440+
for number in before:
441+
after.append(number)
442+
print(after)
443+
```
423444

424-
3. Can you limit the number of attempts to 3?
445+
The answers are [here](answers.md#loops)
425446

426447
***
427448

0 commit comments

Comments
 (0)