Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions concepts/loops/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ The keywords `break`, `continue`, and `else` help customize loop behavior.
The basic [`for`][for statement] `loop` in Python is better described as a _`for each`_ which cycles through the values of any [iterable object][iterable], terminating when there are no values returned from calling [`next()`][next built-in] (_raising a [`StopIteration`][stopiteration]_).

```python

>>> word_list = ["bird", "chicken", "barrel", "bongo"]

>>> for word in word_list:
Expand Down Expand Up @@ -95,7 +94,6 @@ Interestingly, `range()` [_is not an iterator_][range is not an iterator], and c
If both values and indexes are needed, the built-in [`enumerate(<iterable>)`][enumerate] will return an [`iterator`][iterator] over (`index`, `value`) pairs:

```python

>>> word_list = ["bird", "chicken", "barrel", "apple"]

# *index* and *word* are the loop variables.
Expand Down Expand Up @@ -152,15 +150,15 @@ The `enumerate(<iterable>)` function can also be set to `start` the index count
The [`continue`][continue statement] keyword can be used to skip forward to the next iteration cycle:

```python
word_list = ["bird", "chicken", "barrel", "bongo", "sliver", "apple", "bear"]

# This will skip *bird*, at index 0
for index, word in enumerate(word_list):
if index == 0:
continue
if word.startswith("b"):
print(f"{word.title()} (at index {index}) starts with a b.")

>>> word_list = ["bird", "chicken", "barrel", "bongo", "sliver", "apple", "bear"]
...
... # This will skip *bird*, at index 0
... for index, word in enumerate(word_list):
... if index == 0:
... continue
... if word.startswith("b"):
... print(f"{word.title()} (at index {index}) starts with a b.")
...
'Barrel (at index 2) starts with a b.'
'Bongo (at index 3) starts with a b.'
'Bear (at index 6) starts with a b.'
Expand All @@ -176,9 +174,9 @@ The [`break`][break statement] (_like in many C-related languages_) keyword can
... if word.startswith("b"):
... print(f"{word.title()} (at index {index}) starts with a B.")
... elif word == "sliver":
... break
... break
... else:
... print(f"{word.title()} doesn't start with a B.")
... print(f"{word.title()} doesn't start with a B.")
... print("loop broken.")
...
'Bird (at index 0) starts with a B.'
Expand All @@ -202,11 +200,11 @@ The loop [`else` clause][loop else] is unique to Python and can be used for "wra
... word = word.title()
... if word.startswith("B"):
... print(f"{word} (at index {index}) starts with a B.")

...# This executes once *StopIteration* is raised and
...# there are no more items to iterate through.
...# Note the indentation, which lines up with the for keyword.
...else:
...
... # This executes once *StopIteration* is raised and
... # There are no more items to iterate through.
... # Note the indentation, which lines up with the for keyword.
... else:
... print(f"Found the above b-words, out of {len(word_list)} words in the word list.")
...
'Bird (at index 0) starts with a B.'
Expand All @@ -227,7 +225,7 @@ The loop [`else` clause][loop else] is unique to Python and can be used for "wra

... # This statement does not run, because a *break* was triggered.
... else:
... print(f"Found the above b-words, out of {len(word_list)} words in the word list.")
... print(f"Found the above b-words, out of {len(word_list)} words in the word list.")
...
'Bird (at index 0) starts with a B.'
'Barrel (at index 2) starts with a B.'
Expand Down
10 changes: 5 additions & 5 deletions exercises/concept/making-the-grade/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

## General

- [`while`][while-loops] loops are used for _indefinite_ (uncounted) iteration
- [`for`][for-loops] loops are used for _definite_, (counted) iteration.
- [`while`][while-loops] loops are used for _indefinite_ (uncounted) iteration.
- [`for`][for-loops] loops are used for _definite_ (counted) iteration.
- The keywords [`break` and `continue`][control flow] help customize loop behavior.
- [`range(<start>, stop, <step>)`][range] can be used to generate a sequence for a loop counter.
- [`range(<start>, <stop>, <step>)`][range] can be used to generate a sequence for a loop counter.
- The built-in [`enumerate()`][enumerate] will return (`<value>`, `<index>`) pairs to iterate over.

Also being familiar with the following can help with completing the tasks:

- [`lists`][list]: indexing, nested lists, [`<list>.append`][append and pop], [`<list>.pop()`][append and pop].
- [`lists`][list]: indexing, nested lists, [`<list>.append()`][append and pop], [`<list>.pop()`][append and pop].
- [`str`][str]: `str()` constructor, using the `+` to concatenate strings, optionally, [`f-strings`][f-strings].

## 1. Rounding Scores
Expand All @@ -22,7 +22,7 @@ Also being familiar with the following can help with completing the tasks:
## 2. Non-Passing Students

- There's no need to declare `loop` counters or `index` counters when iterating through an object using a `for` loop.
- A results counter does need to be set up and _incremented_ -- you'll want to `return` the count of non-passing students when the loop terminates.
- A results counter does need to be set up and _incremented_ you'll want to `return` the count of non-passing students when the loop terminates.

## 3. The "Best"

Expand Down
8 changes: 4 additions & 4 deletions exercises/concept/making-the-grade/.meta/exemplar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def round_scores(student_scores):
"""Round all provided student scores.

Parameters:
student_scores (list[float|int]): Student exam scores.
student_scores (list[float]): Student exam scores.

Returns:
list[int]: Student scores *rounded* to the nearest integer value.
Expand Down Expand Up @@ -42,7 +42,7 @@ def above_threshold(student_scores, threshold):
threshold (int): The threshold to cross to be the "best" score.

Returns:
list[int]: Integer scores that are at or above the "best" threshold.
list[int]: Integer scores that are at or above the "best" threshold.
"""

above = []
Expand All @@ -57,7 +57,7 @@ def letter_grades(highest):
"""Create a list of grade thresholds based on the provided highest grade.

Parameters:
highest: int - value of the highest exam score.
highest (int): The value of the highest exam score.

Returns:
list[int]: Lower threshold scores for each D-A letter grade interval.
Expand Down Expand Up @@ -85,7 +85,7 @@ def student_ranking(student_scores, student_names):
student_names (list[str]): Student names by exam score in descending order.

Returns:
list[str]: Strings in format ["<rank>. <student name>: <score>"].
list[str]: Strings in format ["<rank>. <student name>: <score>"].
"""

results = []
Expand Down
8 changes: 4 additions & 4 deletions exercises/concept/making-the-grade/loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def round_scores(student_scores):
"""Round all provided student scores.

Parameters:
student_scores (list[float|int]): Student exam scores.
student_scores (list[float]): Student exam scores.

Returns:
list[int]: Student scores *rounded* to the nearest integer value.
Expand Down Expand Up @@ -35,7 +35,7 @@ def above_threshold(student_scores, threshold):
threshold (int): The threshold to cross to be the "best" score.

Returns:
list[int]: Integer scores that are at or above the "best" threshold.
list[int]: Integer scores that are at or above the "best" threshold.
"""

pass
Expand All @@ -45,7 +45,7 @@ def letter_grades(highest):
"""Create a list of grade thresholds based on the provided highest grade.

Parameters:
highest: int - value of the highest exam score.
highest (int): The value of the highest exam score.

Returns:
list[int]: Lower threshold scores for each D-A letter grade interval.
Expand All @@ -69,7 +69,7 @@ def student_ranking(student_scores, student_names):
student_names (list[str]): Student names by exam score in descending order.

Returns:
list[str]: Strings in format ["<rank>. <student name>: <score>"].
list[str]: Strings in format ["<rank>. <student name>: <score>"].
"""

pass
Expand Down
Loading