Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 52b40d6

Browse files
authoredSep 1, 2022
Merge pull request #98 from jetbrains-academy/sofia/second_else_with_loops_task
Added another task for else with loops to cover more concepts
2 parents 41f75bb + d8e6292 commit 52b40d6

File tree

7 files changed

+106
-0
lines changed

7 files changed

+106
-0
lines changed
 

‎Loops/Else with loops 2/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 35
7+
length: 184
8+
placeholder_text: pass
9+
- name: tests/test_task.py
10+
visible: false
11+
- name: __init__.py
12+
visible: false
13+
- name: tests/__init__.py
14+
visible: false

‎Loops/Else with loops 2/task.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Else with loops 2
2+
3+
This type of `else` is useful only if there is an `if` condition present inside the loop, which somehow depends on the loop variable.
4+
Let's look once again at the example from the previous task:
5+
6+
```python
7+
for n in range(2, 10):
8+
for x in range(2, n):
9+
if n % x == 0:
10+
print(n, 'equals', x, '*', n//x)
11+
break
12+
else:
13+
# loop fell through without finding a factor
14+
print(n, 'is a prime number')
15+
```
16+
17+
The `else` statement will only be executed if `n` is a prime number, i.e. the `if` statement has not been executed for any iteration of the inner
18+
`for` loop.
19+
20+
### Task
21+
22+
Inside the [function](course://Functions/Definition) `contains_even_number()`, write a `for` loop that would iterate over a list `lst` and, if an even element is found,
23+
print `f"List {lst} contains an even number."` and exit the loop, if no such element is found, print `f"List {lst} does not contain an even number."`.
24+
25+
<div class="hint">
26+
27+
You can use `if i % 2 == 0:` to test if a given number is even.
28+
</div>
29+
30+
<div class="hint">
31+
32+
Use the `break` statement to exit the loop if an even number is found.
33+
</div>
34+
35+
<div class="hint">
36+
37+
Use an `else` clause to print `f"List {lst} does not contain an even number."`.
38+
</div>

‎Loops/Else with loops 2/task.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def contains_even_number(lst):
2+
for i in lst:
3+
if i % 2 == 0:
4+
print(f"List {lst} contains an even number.")
5+
break
6+
else:
7+
print(f"List {lst} does not contain an even number.")
8+
9+
10+
contains_even_number([1, 9, 8])
11+
contains_even_number([1, 3, 5])

‎Loops/Else with loops 2/tests/__init__.py

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import unittest
2+
import contextlib
3+
import io
4+
5+
6+
from task import contains_even_number
7+
8+
9+
class TestCase(unittest.TestCase):
10+
def test_even(self):
11+
f = io.StringIO()
12+
13+
with contextlib.redirect_stdout(f):
14+
contains_even_number([1, 9, 37, 4])
15+
output = f.getvalue().split('\n')
16+
self.assertEqual(['List [1, 9, 37, 4] contains an even number.', ''], output, msg="Incorrect result for a "
17+
"list with an even number.")
18+
19+
def test_even_several_times(self):
20+
f = io.StringIO()
21+
22+
with contextlib.redirect_stdout(f):
23+
contains_even_number([1, 9, 8, 8, 8])
24+
output = f.getvalue().split('\n')
25+
self.assertEqual(['List [1, 9, 8, 8, 8] contains an even number.', ''], output, msg="For a list with any "
26+
"number of even values "
27+
"the phrase 'List {lst} "
28+
"contains an even number' "
29+
"should only be printed "
30+
"once. You might have "
31+
"missed a break "
32+
"statement.")
33+
34+
def test_odd(self):
35+
f = io.StringIO()
36+
37+
with contextlib.redirect_stdout(f):
38+
contains_even_number([1, 9, 37, 5])
39+
output = f.getvalue().split('\n')
40+
self.assertEqual(['List [1, 9, 37, 5] does not contain an even number.', ''], output, msg="Incorrect result "
41+
"for a list without"
42+
" an even number.")

‎Loops/lesson-info.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ content:
88
- Break keyword
99
- Fix infinite execution
1010
- Else with loops
11+
- Else with loops 2
1112
- Continue keyword

0 commit comments

Comments
 (0)
Please sign in to comment.