-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Submitting examples for code review for python-break examples #634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
69d2c6c
30b6d8c
763c726
38a59b7
f4d14af
4fa269a
e592cfc
62be6d0
b393891
1c08fe0
de36639
415c5cb
d5ad229
f62583d
63c4945
b41931b
6d9e6fc
3f84ac7
01b96ec
4369b21
8d58950
8ad2335
ee55cc1
e25248d
893f429
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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/). |
| 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 | ||
| 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)) | ||
|
dphoenix marked this conversation as resolved.
Outdated
|
||
| 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]] | ||
|
dphoenix marked this conversation as resolved.
Outdated
|
||
|
|
||
| failed_score = 60 | ||
| num_failed_students = 0 | ||
| for score_set in scores: | ||
| for score in score_set: | ||
|
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)) | ||
| 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) | ||
| ) | ||
|
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.") | ||
|
dphoenix marked this conversation as resolved.
Outdated
|
||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.