diff --git a/how-to-exit-loops-early-with-python-break-keyword/README.md b/how-to-exit-loops-early-with-python-break-keyword/README.md new file mode 100644 index 0000000000..e41f38d8f4 --- /dev/null +++ b/how-to-exit-loops-early-with-python-break-keyword/README.md @@ -0,0 +1,3 @@ +# How to Exit Loops Early With the Python Break Keyword + +This folder provides the code examples for the Real Python tutorial [How to Exit Loops Early With the Python Break Keyword](https://realpython.com/python-break/). diff --git a/how-to-exit-loops-early-with-python-break-keyword/break_statement_introduction.py b/how-to-exit-loops-early-with-python-break-keyword/break_statement_introduction.py new file mode 100644 index 0000000000..ed394379e6 --- /dev/null +++ b/how-to-exit-loops-early-with-python-break-keyword/break_statement_introduction.py @@ -0,0 +1,4 @@ +for number in range(10): + if number > 5: + break + print(number) diff --git a/how-to-exit-loops-early-with-python-break-keyword/continue_statement_example.py b/how-to-exit-loops-early-with-python-break-keyword/continue_statement_example.py new file mode 100644 index 0000000000..b437ca25fd --- /dev/null +++ b/how-to-exit-loops-early-with-python-break-keyword/continue_statement_example.py @@ -0,0 +1,4 @@ +for index in range(6): + if index % 2 == 0: + continue + print(index) diff --git a/how-to-exit-loops-early-with-python-break-keyword/for_loop_test_scores_example.py b/how-to-exit-loops-early-with-python-break-keyword/for_loop_test_scores_example.py new file mode 100644 index 0000000000..d8bb5943f8 --- /dev/null +++ b/how-to-exit-loops-early-with-python-break-keyword/for_loop_test_scores_example.py @@ -0,0 +1,8 @@ +scores = [90, 30, 50, 70, 85, 35] + +num_failed_scores = 0 +failed_score = 60 +for score in scores: + if score < failed_score: + num_failed_scores += 1 +print(f"Number of failed tests: {num_failed_scores}") diff --git a/how-to-exit-loops-early-with-python-break-keyword/for_loop_test_scores_example_with_tutor_rec.py b/how-to-exit-loops-early-with-python-break-keyword/for_loop_test_scores_example_with_tutor_rec.py new file mode 100644 index 0000000000..a06273c343 --- /dev/null +++ b/how-to-exit-loops-early-with-python-break-keyword/for_loop_test_scores_example_with_tutor_rec.py @@ -0,0 +1,13 @@ +scores = [90, 30, 50, 70, 85, 35] + +num_failed_scores = 0 +failed_score = 60 +needs_tutoring = "No" +for score in scores: + if score < failed_score: + num_failed_scores += 1 + if num_failed_scores >= 2: + needs_tutoring = "Yes" + break + +print(f"Does the student need tutoring? {needs_tutoring}") diff --git a/how-to-exit-loops-early-with-python-break-keyword/nested_loop_number_of_failed_students_example.py b/how-to-exit-loops-early-with-python-break-keyword/nested_loop_number_of_failed_students_example.py new file mode 100644 index 0000000000..509c6127a5 --- /dev/null +++ b/how-to-exit-loops-early-with-python-break-keyword/nested_loop_number_of_failed_students_example.py @@ -0,0 +1,11 @@ +scores = [[90, 30, 80, 100], [100, 80, 95, 87], [75, 84, 77, 50]] + +failed_score = 60 +num_failed_students = 0 +for student_score_list in scores: + for score in student_score_list: + if score < failed_score: + num_failed_students += 1 + break + +print(f"Number of students who failed a test: {num_failed_students}") diff --git a/how-to-exit-loops-early-with-python-break-keyword/user_input_example.py b/how-to-exit-loops-early-with-python-break-keyword/user_input_example.py new file mode 100644 index 0000000000..c26b90402d --- /dev/null +++ b/how-to-exit-loops-early-with-python-break-keyword/user_input_example.py @@ -0,0 +1,24 @@ +import random + +guesses_left = 4 +random_number = random.randint(1, 10) + +while True: + if guesses_left <= 0: + print( + f"You ran out of guesses! The correct number was {random_number}" + ) + 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()): + print("Please enter a valid value.") + elif int(guess) == random_number: + print("Congratulations, you picked the correct number!") + break + else: + print("Sorry, your guess was incorrect.") + guesses_left -= 1 + print(f"You have {guesses_left} guesses left.")