Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
69d2c6c
Add files via upload
dphoenix Feb 4, 2025
30b6d8c
Updated for-loop code example
dphoenix Feb 4, 2025
763c726
While loop example
dphoenix Feb 4, 2025
38a59b7
Uploaded new example
dphoenix Feb 4, 2025
f4d14af
Add files via upload
dphoenix Feb 4, 2025
4fa269a
Make linter happy
acsany Feb 5, 2025
e592cfc
Add README file
acsany Feb 5, 2025
62be6d0
Merge branch 'master' into master
acsany Feb 5, 2025
b393891
Update for_loop_test_scores_example.py
dphoenix Feb 6, 2025
1c08fe0
Update for_loop_test_scores_example.py
dphoenix Feb 6, 2025
de36639
Update nested_loop_number_of_failed_students_example.py
dphoenix Feb 7, 2025
415c5cb
Update user_input_example.py
dphoenix Feb 7, 2025
d5ad229
Update user_input_example.py
dphoenix Feb 7, 2025
f62583d
Update nested_loop_number_of_failed_students_example.py
dphoenix Feb 8, 2025
63c4945
Use black formatting
acsany Feb 13, 2025
b41931b
Create break_statement_introduction.py
dphoenix Mar 17, 2025
6d9e6fc
Changed directory name to match article slug, and updated code samples
dphoenix Mar 17, 2025
3f84ac7
Merge branch 'master' into master
dphoenix Mar 17, 2025
01b96ec
Update break_statement_introduction.py
dphoenix Mar 17, 2025
4369b21
Update continue_statement_example.py
dphoenix Mar 17, 2025
8d58950
Update for_loop_test_scores_example.py
dphoenix Mar 17, 2025
8ad2335
Update README.md
brendaweles Apr 2, 2025
ee55cc1
Merge branch 'master' into master
bzaczynski Apr 3, 2025
e25248d
Update user_input_example.py
bzaczynski Apr 3, 2025
893f429
Update nested_loop_number_of_failed_students_example.py
bzaczynski Apr 3, 2025
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
3 changes: 3 additions & 0 deletions python-break/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python break: Exit Loops Early With Python's Break Keyword

This folder provides the code examples for the Real Python tutorial [Python break: Exit Loops Early With Python's Break Keyword](https://realpython.com/python-break/).
15 changes: 15 additions & 0 deletions python-break/for_loop_test_scores_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Use case 1: If the student fails 2 or more tests,
# the student must go to tutoring (for-loop)
scores = [90, 30, 50, 70, 85, 35]

num_failed_scores = 0
failed_score = 60
needs_utoring = False
Comment thread
dphoenix marked this conversation as resolved.
Outdated
for score in scores:
if score < failed_score:
num_failed_scores += 1
if num_failed_scores >= 2:
needs_tutoring = True
break

print("Does the student need tutoring? " + str(needs_tutoring))
Comment thread
dphoenix marked this conversation as resolved.
Outdated
11 changes: 11 additions & 0 deletions python-break/nested_loop_number_of_failed_students_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Use case 4: Check to see how many students have failed a test (nested loop)
scores = [[90, 30, 80, 100], [100, 80, 95, 87], [75, 84, 77, 90]]
Comment thread
dphoenix marked this conversation as resolved.
Outdated

failed_score = 60
num_failed_students = 0
for score_set in scores:
for score in score_set:
Comment thread
dphoenix marked this conversation as resolved.
Outdated
if score < failed_score:
num_failed_students += 1
break
print("Number of students who failed a test: " + str(num_failed_students))
29 changes: 29 additions & 0 deletions python-break/user_input_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Use case 3: Getting user input

# Import the random module for generating random numbers
import random

guesses_left = 4
random_number = random.randint(1, 10)

while True:
if guesses_left <= 0:
print(
"You ran out of guesses! The correct number was "
+ str(random_number)
)
Comment thread
dphoenix marked this conversation as resolved.
Outdated
break
guess = input("Guess a number between 1 and 10, or enter q to quit:")
if guess == "q":
print("Successfully exited game.")
break
elif not (guess.isnumeric()):
guess = input("Please enter a valid value: ")
else:
if int(guess) == random_number:
print("Congratulations, you picked the correct number!")
break
else:
print("Sorry, your guess was incorrect.")
guesses_left -= 1
print("You have " + str(guesses_left) + " guesses left.")
Comment thread
dphoenix marked this conversation as resolved.
Outdated
11 changes: 11 additions & 0 deletions python-break/while_loop_print_test_scores_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Use case 2: Print out the score of the first five tests (while loop)
scores = [90, 30, 50]
i = 0

while i < 5:
if i > len(scores) - 1:
# If there are less than 5 scores,
# break out of the loop when all scores are printed
break
print("Score: " + str(scores[i]))
i += 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this example. Generally, you'd instead use a for loop for this (like in the first example.)

Suggested change
# Use case 2: Print out the score of the first five tests (while loop)
scores = [90, 30, 50]
i = 0
while i < 5:
if i > len(scores) - 1:
# If there are less than 5 scores,
# break out of the loop when all scores are printed
break
print("Score: " + str(scores[i]))
i += 1
# Use case 2: Check if a student passes an exam
points = [10, 15, 3, 12, 14, 9]
max_score = len(points) * 15
min_score_to_pass = max_score / 2
student_score = 0
for score in points:
student_score += score
if student_score >= min_score_to_pass:
print("Congratulations, you passed the exam!")
break

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acsany Point taken. Do you think we should just take this one out? But regarding the suggested change, the task that the code performs from the user's perspective is different, but I don't know that the concept this amended code demonstrates is so different from use case #1. And since the user input example has a while-loop, we're meeting the intended original purpose of this example, which was to show a while/break combination. Would this updated use case example be redundant now?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'd say, let's keep this example out and only take it in if you feel you need another example to explain break.