0% found this document useful (0 votes)
4 views5 pages

33 Textbook: Intro to Python Textbook | CodeHS

Chapter 3 of the Intro to Python textbook covers key concepts related to looping, including while loops, for loops, and the use of break and continue keywords to control loop behavior. It provides examples demonstrating how to exit loops early with break and skip iterations with continue, along with exercises for practical application. The chapter emphasizes the importance of correctly implementing these structures to avoid infinite loops.

Uploaded by

xilifo7836
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

33 Textbook: Intro to Python Textbook | CodeHS

Chapter 3 of the Intro to Python textbook covers key concepts related to looping, including while loops, for loops, and the use of break and continue keywords to control loop behavior. It provides examples demonstrating how to exit loops early with break and skip iterations with continue, along with exercises for practical application. The chapter emphasizes the importance of correctly implementing these structures to avoid infinite loops.

Uploaded by

xilifo7836
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Intro to Python Textbook

(/textbook/intropython_textbook/) CHAPTER 3

1. Python and Console Interaction Looping


(/textbook/intropython_textbook/1)
2. Conditionals
(/textbook/intropython_textbook/2)
3. Looping
(/textbook/intropython_textbook/3)
3.1 While Loops
(/textbook/intropython_textbook/3.1)
3.2 For Loops
(/textbook/intropython_textbook/3.2) 3.3 Break and Continue
3.3 Break and Continue
(/textbook/intropython_textbook/3.3)
 Break
Break

Positive Break
In this lesson, you will learn about a couple of Python keywords
that can be used to alter the behavior of a loop. While loops can
Continue
be very helpful when we need to iterate code multiple times,
Continue Past 5
there are situations where you want to exit the loop in the middle
Bike Frame Size
of an iteration.
Loop and a Half
To demonstrate this, take a look at the example below:
Check Your Understanding

Exercise: Higher/Lower 1 x = -1
2 while x < 0:
3.4 Nested Control Structures
3 x = int(input("Number: "))
(/textbook/intropython_textbook/3.4)
4 print("Number must be positive!")
4. Functions and Exceptions

If the user enters -5 , the “Number must be positive!” prints and


the loop repeats, which is the behavior you’d expect. However, if
the user enters 10 , the print function on line 4 will still execute,
even though a positive number was entered! This happens
because the loop condition ( x < 0 ) isn’t checked until all of the
code inside of the loop runs. You can imagine this would be
confusing for a user.
In order to solve this problem – be able to exit the loop as soon
as a positive number is entered – you can use the keyword
break . When running a program, as soon as the Python
interpreter hits a break statement inside of a loop, the loop is
immediately exited and none of the remaining code in the loop
will be executed!

 Positive Break
Below is a similar example as above, but with a few tweaks:
Instead of putting the x <= 0 condition in the while loop, an
if statement with the condition is placed immediately after
the user enters a number. This allows the program to check
for positive numbers as soon as the user enters them.
for positive numbers as soon as the user enters them.
If the user-entered number is positive, the break statement
inside of the if statement will execute, forcing the while loop
to end immediately. Notice that the print function does not
execute in this case, which is what you want!
The condition of the while loop is set to True . This allows
the user to keep entering numbers until they enter a positive
one!

 Continue
Alternatively, the continue keyword allows the program to skip
the rest of the loop iteration (that is, skip the remaining code in
the loop) and jump to the top of the loop for the next iteration.
Review the example below to see this structure:

code before the loop


while condition:
if something:
continue
code that will be skipped with a continue

code after the loop...

 Continue Past 5
While i still iterates through numbers 0 - 9, this program only
prints 0 - 4 and 6 - 9 (NOT 5). Try changing to if condition to see
how the continue statement affects the printed output!
 Bike Frame Size
This program asks a user for the size of their bike frame and
determines if the size fits into the available constraints.
Using while True: would usually be an infinite loop, since True
is always true! But with the use of break , you can force it to exit
at a specific point.
Think about what would happen if you changed the break
statement to a continue statement. (Hint: CAUTION).
 Loop and a Half
This program continually asks a user to guess a number until
they have guessed the magic number.

 Check Your Understanding


 Exercise: Higher/Lower
Write a program that makes the user guess a particular number
between 1 and 100. Save the number to be guessed in a variable
called secret_number .
If the user guesses a number higher than the secret number, you
should say Too high! . Similarly, you should say Too low! if they
guess a number lower than the secret number. Once they guess
the number, say Correct!
Here is an example run of your final program, assuming that your
number is 3 :

Enter a guess: 5
Too high!
Enter a guess: 2
Too low!
Enter a guess: 4
Too high!
Enter a guess: 3
Correct!

Use the loop and a half structure for your program. You should
have a while loop that looks something like this:

while True:
...

if ...:
break

...

Be careful - you may end up with an infinite loop in your program


if you don’t break correctly! If you run a program with an infinite
loop in it, the page might freeze and you might have to refresh it
to continue.

You might also like