π while
loops
December 21, 2016
Do Now
# what do you think this will do?
n=1
while n>0:
print n
n = n+1
bad stuff
infinite loop! 1
β while Loop
Code that repeats while a condition is true.
x=1
while x in range(0,4):
print x
x = x+1
Letβs take a closer look:
n=1 # sets n to 1. What happens if we don't?
while n>0:
print(n)
n = n+1
# Predict!
while True:
print("whoa")
# What about this? What gives?
while x<5:
print(x)
x = x + 1
meanwhile
β¦
In π CS9 Python:
It takes any number as input, starts counting down and ends with a BOOM! (or similar exclamation.)
It should look like this:
Enter number: 10
10
9
8
7
6
5
4
3
2
1
BOOM!
-
π Congratulations! You now have the power to crash Google Chrome. ↩