Skip to content

Commit a34ab5c

Browse files
committed
exercises inspired by grimey_grim
1 parent 98d1f49 commit a34ab5c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

basics/answers.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,29 @@ isn't exactly like mine but it works just fine it's ok, and you can
249249
print(after)
250250
```
251251
252+
3. The code has some empty lines in it, and they divide it nicely into
253+
three parts. All of these parts have some problems, so I'll go
254+
through them one by one.
255+
256+
The first part makes a variable called `input`. The problem is that
257+
now the rest of the program [can't use the input
258+
function](using-functions.md#variables-names-and-builtin-things). It
259+
doesn't really matter here because the rest of the program doesn't
260+
use it anyway, but I still recommend using some other variable name,
261+
like `inputlist`.
262+
263+
The second part runs `numbers = []` three times. It was probably
264+
meant to be ran once before the loop started, like this:
265+
266+
```py
267+
numbers = []
268+
for string in inputlist:
269+
numbers.append(int(string))
270+
```
271+
272+
The third part calculates `result + n` but throws away the result.
273+
It was probably supposed to do `result += n` instead.
274+
252275
## Trey Hunner: zip and enumerate
253276
254277
1. Read some lines with `input` into a list and then enumerate it.

basics/loops.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,23 @@ while True:
442442
print(after)
443443
```
444444

445+
3. This program is supposed to convert everything in a list to integers
446+
and then calculate their sum. It should print 6 because `1 + 2 + 3`
447+
is 6. Fix the program.
448+
449+
```py
450+
input = ['1', '2', '3']
451+
452+
for string in input:
453+
numbers = []
454+
numbers.append(int(string))
455+
456+
result = 0
457+
for n in numbers:
458+
result + n
459+
print("their sum is", result)
460+
```
461+
445462
The answers are [here](answers.md#loops)
446463

447464
***

0 commit comments

Comments
 (0)