Skip to content

Commit d80539b

Browse files
committed
Merge branch 'python-selenium' of https://github.com/realpython/materials into python-selenium
2 parents 7f22ab0 + 4d0304f commit d80539b

7 files changed

+67
-0
lines changed

python-break/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Exit Loops Early With the Python Break Keyword
2+
3+
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/).
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for number in range(10):
2+
if number > 5:
3+
break
4+
print(number)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for index in range(6):
2+
if index % 2 == 0:
3+
continue
4+
print(index)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
scores = [90, 30, 50, 70, 85, 35]
2+
3+
num_failed_scores = 0
4+
failed_score = 60
5+
for score in scores:
6+
if score < failed_score:
7+
num_failed_scores += 1
8+
print(f"Number of failed tests: {num_failed_scores}")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
scores = [90, 30, 50, 70, 85, 35]
2+
3+
num_failed_scores = 0
4+
failed_score = 60
5+
needs_tutoring = "No"
6+
for score in scores:
7+
if score < failed_score:
8+
num_failed_scores += 1
9+
if num_failed_scores >= 2:
10+
needs_tutoring = "Yes"
11+
break
12+
13+
print(f"Does the student need tutoring? {needs_tutoring}")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
scores = [[90, 30, 80, 100], [100, 80, 95, 87], [75, 84, 77, 50]]
2+
3+
failed_score = 60
4+
num_failed_students = 0
5+
for student_score_list in scores:
6+
for score in student_score_list:
7+
if score < failed_score:
8+
num_failed_students += 1
9+
break
10+
11+
print(f"Number of students who failed a test: {num_failed_students}")

python-break/user_input_example.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import random
2+
3+
guesses_left = 4
4+
random_number = random.randint(1, 10)
5+
6+
while True:
7+
if guesses_left <= 0:
8+
print(
9+
f"You ran out of guesses! The correct number was {random_number}"
10+
)
11+
break
12+
guess = input("Guess a number between 1 and 10, or enter q to quit: ")
13+
if guess == "q":
14+
print("Successfully exited game.")
15+
break
16+
elif not (guess.isnumeric()):
17+
print("Please enter a valid value.")
18+
elif int(guess) == random_number:
19+
print("Congratulations, you picked the correct number!")
20+
break
21+
else:
22+
print("Sorry, your guess was incorrect.")
23+
guesses_left -= 1
24+
print(f"You have {guesses_left} guesses left.")

0 commit comments

Comments
 (0)