Skip to content

Conversation

@dphoenix
Copy link
Contributor

@dphoenix dphoenix commented Feb 4, 2025

Where to put new files:

  • New files should go into a top-level subfolder, named after the article slug. For example: my-awesome-article

How to merge your changes:

  1. Make sure the CI code style tests all pass (+ run the automatic code formatter if necessary).
  2. Find an RP Team member on Slack and ask them to review & approve your PR.
  3. Once the PR has one positive ("approved") review, GitHub lets you merge the PR.
  4. 🎉

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
@dphoenix
Copy link
Contributor Author

dphoenix commented Feb 4, 2025

@acsany Hello! I made some edits and uploaded my updated code examples

@acsany acsany self-requested a review February 5, 2025 11:43
Copy link
Contributor

@acsany acsany left a comment

Choose a reason for hiding this comment

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

Hi @dphoenix!
Thanks for providing the code examples and creating the PR. Please have a look at my comments below 🙂

Comment on lines 1 to 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
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
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
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.

- 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
@dphoenix
Copy link
Contributor Author

dphoenix commented Feb 7, 2025

@acsany I need a little help with the linter. For my latest job, I got this message:
All checks passed!
would reformat /home/runner/work/materials/materials/python-break/user_input_example.py

Oh no! 💥 💔 💥
1 file would be reformatted, 3319 files would be left unchanged.
Error: Process completed with exit code 1.

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!

@dphoenix
Copy link
Contributor Author

@acsany Created the pull request for my updated materials/code samples

@bzaczynski bzaczynski merged commit cb40148 into realpython:master Apr 3, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants