Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions python-break/for_loop_test_scores_example.py
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))
11 changes: 11 additions & 0 deletions python-break/nested_loop_number_of_failed_students_example.py
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]]

failed_score = 60
num_failed_students = 0
for score_set in scores:
for score in score_set:
if score < failed_score:
num_failed_students += 1
break
print("Number of students who failed a test: " + str(num_failed_students))
29 changes: 29 additions & 0 deletions python-break/user_input_example.py
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)
)
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.")
11 changes: 11 additions & 0 deletions python-break/while_loop_print_test_scores_example.py
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
Loading