Submitting examples for code review for python-break examples#634
Submitting examples for code review for python-break examples#634bzaczynski merged 25 commits intorealpython:masterfrom
Conversation
Changed variable names to use underscores, rather than camel-case
Breaking out of a while loop early. Code example prints out first five (or less) test scores.
Code example for getting user input
Nested loop example, which prints out the number of students who got at least one failed score
|
@acsany Hello! I made some edits and uploaded my updated code examples |
| # 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 |
There was a problem hiding this comment.
I'm not a fan of this example. Generally, you'd instead use a for loop for this (like in the first example.)
| # 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 |
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
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.
Fixed a variable typo
- Changed True/False values to "Yes"/"No", to make output more reader-friendly - Used f-string for string interpolation
Used f-strings for string interpolation
Removed unnecessary nested loop
|
@acsany I need a little help with the linter. For my latest job, I got this message: Oh no! 💥 💔 💥 But I don't see anything indicating how I would need to fix this file. Can you help me understand what it's asking me to fix? Thanks so much! |
Created a more meaningful variable name
|
@acsany Created the pull request for my updated materials/code samples |
Where to put new files:
my-awesome-articleHow to merge your changes: