Skip to content

Commit ab4a7a8

Browse files
committed
exercise based on horusr's problem
1 parent 7d5129f commit ab4a7a8

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

basics/answers.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,23 @@ isn't exactly like mine but it works just fine it's ok, and you can
272272
The third part calculates `result + n` but throws away the value.
273273
It was probably supposed to do `result += n` instead.
274274
275+
4. If you run this program you'll notice that nothing happened to the
276+
numbers list. The reason is that the `number` variable only works
277+
one way. It gets its values from the `numbers` list, but changing it
278+
doesn't change the `numbers` list. In general, `thing = stuff`
279+
changes the `thing` variable, and that's it. It doesn't change
280+
anything else.
281+
282+
An easy solution is to just create a new list:
283+
284+
```python
285+
numbers = ['1', '2', '3']
286+
converted_numbers = []
287+
for number in numbers:
288+
converted_numbers.append(int(number))
289+
print(converted_numbers)
290+
```
291+
275292
## Trey Hunner: zip and enumerate
276293
277294
1. Read some lines with `input` into a list and then enumerate it.

basics/loops.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ while True:
432432
print(thing)
433433
```
434434

435-
2. This code is supposed to print `[1, 2, 3, 4, 5, 6]`. Fix it without
435+
2. This code is supposed to print `[1, 2, 3, 4, 5, 6]`. Fix it without
436436
changing the `before` list.
437437

438438
```python
@@ -460,7 +460,16 @@ while True:
460460
print("their sum is", result)
461461
```
462462

463-
The answers are [here](answers.md#loops)
463+
4. This program is supposed to print `[1, 2, 3]`. Fix it.
464+
465+
```python
466+
numbers = ['1', '2', '3']
467+
for number in number:
468+
number = int(number)
469+
print(numbers)
470+
```
471+
472+
The answers are [here](answers.md#loops).
464473

465474
***
466475

0 commit comments

Comments
 (0)