Jed Rembold
September 12, 2025
Example: if n=0, then the
x % n == 0 is never actually checked in the
statement
n != 0 and x % n == 0
since n != 0 already is
False and
False and anything is always
False
x % n == 0 statement would have erred out if
n=0What value is printed when the code below runs?
TrueFalseA = 10
B = 4
C = 1
A *= B
if A > 40 and C != 10 % 4:
print(B+C)
elif A ** B <= C:
print(A * C)
else:
print(A < B or not (C == 10 // 4))
range() iterablerange() function handles
this and produces the needed iterable objectBe careful, the range function will stop
one step before the final stop value.
We have already seen for loops in
Karel, but we can expand on them a bit now
The more general syntax of a for loop
looks like:
for |||variable||| in |||sequence|||:
|||code to loop over|||
|||variable||| is a variable name that
will be assigned every value in the sequence over the course of the
loop|||sequence||| is any expression that
produces an object which supports iteration
range() fulfills this
role, and thus produces an iterable sequenceProviding just a stop argument:
for n in range(5):
print(n)Providing a start and stop:
for n in range(1,11):
print(n)Providing a start, stop, and step:
for n in range(10,0,-1):
print(n)0
1
2
3
4
1
2
3
4
5
6
7
8
9
10
10
9
8
7
6
5
4
3
2
1
Which of the below blocks of code would print something different from the others?
for n in range(10):
if n % 2 == 0:
if n <= 10:
print(n)
for k in range(0,10):
if not (k % 2 > 0):
print(k)
j = 0
while j < 10:
print(j)
j += 2
for i in range(0,10,2):
if i > 0:
print(i)
Suppose we wanted to write a function to compute the greatest factor of a provided number (not including the number itself)
Algorithm:
def greatest_factor(num):
"""Finds the greatest factor of a number."""
for i in range(num-1,0,-1):
if num % i == 0:
return i
If debugging is the process of removing software bugs, then programming must be the process of putting them in.
Edsger W. Dijkstra
Concentrate on what your program IS doing, instead of what it SHOULD be doing.
Let Python help you: print or log the state of different variables.
Stop and read. The documentation. The error messages.
The rich library offers some very
pretty error messages: install with
pip install rich
At the top of your code, then include:
from rich.traceback import install
install(show_locals=True)Use PythonTutor or a debugger to track EXACTLY what is happening in your program.
Don’t make random changes to your code in the hopes that it will miraculously start working.
Talk it out.
Test your code as you go! Either manually or automatically.
You can use Python’s assert statement
to write test functions, which take the form:
assert |||condition|||
where |||condition||| is any operation
that returns a True or
False
Assert statements “expect” a condition to yield a
True, and if they do, nothing happens
If an assert condition evaluates to
False, an error is raised
Naming your test functions starting with the word
test_ will make them automatically
discoverable by other tools
greatest_factor function from earlierdef test_greatest_factor():
""" Runs several tests on the function greatest_factor """
assert greatest_factor(10) == 5
assert greatest_factor(7) == 1
assert greatest_factor(51) == 17
assert greatest_factor(9) == 3