In addition to
for loops which run for a set number of iterations, Python has another type of loop.
while loops iterate until a particular condition is met.
 Set up
💻 Start by opening the Terminal cloning this lab onto your laptop. As a reminder, we will run this command at the start of each lab.
💻 Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.
👾 💬 Exiting the poetry shell
When you want to exit the shell, you can type
💻 Take a look at the files inside with:
 What is a While Loop?
while loops use conditions just like
if statements. You can use operators to compare
values or to generate
False conditions. Looping until a condition is met
can be useful when you are getting input from a user, generating random variables,
or repeatedly changing a value.
user_input = -1 while user_input < 1 or user_input > 10: user_input = int(input("Tell me a number between 1-10 (inclusive): "))
While True / Break
You can also make
while loops run indefinitely by setting the condition to
while True: This can be useful when you want to loop a program repeatedly.
To stop a loop like this, you can use a
break statement. Once the program reaches the
break, the loop will exit.
You’ve actually already seen an example of this kind of loop when you learned about conditionals in the previous lab.
speed(10) while True: drawing = input("What would you like me to draw? ") size = int(input("How big should I draw it? ")) if drawing == "square": for i in range(4): forward(size) right(90) elif drawing == "quit": break else: print("Sorry, I don't know how to draw that...")
 Guessing Game
One common usage of
while loops is in games. In this first part of the lab, you will be using a
while loop to create a number guessing game.
---------------------------- Guess a number between 1-10! ---------------------------- Guess a number: 5 Guess a number: 10 Guess a number: 3 Correct Guess a number: 8 Guess a number:
It works! But, even after you guess the correct number, the game continues. It’s up to you to fix the code!
👾 💬 Stopping a program💻 Open the file in atom
When you want to stop running a program, type
^Cin the terminal.
The final game should like something like this:
---------------------------- Guess a number between 1-10! ---------------------------- Guess a number: 5 Too high... Guess a number: 3 Too high... Guess a number: 2 Correct
 Hailstone Sequence
[Calculating the Sequence]
Now that you’ve gotten practice with
while loops, you will be exploring a special sequence known as the
This sequence results from the following rules (known as the Collatz conjecture):
- take any positive number
- find the next term of the sequence using the following rules:
nis even, the next term is
nis odd, the next term is
- repeat until
n = 1
The conjecture states that no matter the starting value of
n, the sequences will always reach 1.
This sequence is interesting because though no number has ever been found that doesn’t reach 1,the Collatz conjecture has never been proven. This is an unsolved problem in mathematics!
[Pseudocode the Sequence]
This is another algorithm which will require pseudocode to figure out.
👀 Your program must:
- Ask the user what number the program should calculate the hailstone sequence of.
- Print out each number in the sequence.
- Print out how many steps it took for the sequence to reach
Here are some things to consider:
- This program will require a loop. What kind of loop do you think is best? Remember that for loops run for a definite number of times and while loops run until a condition is met.
- You will need to determine if each term is odd or even. What are some characteristics of even numbers that will help you determine if a number is even?
- In addition to calculating each term, you must count how many steps it takes to reach 1 and report this number at the end.
To get started, open up
comments to complete the pseudocode to find the hailstone sequence of any starting number. There are currently three line of pseudocode in the file.
[Code the Sequence]
💻 Translate your pseudocode into Python code. Once completed, your program will look something like this:
--- Hailstone Sequence --- Input a starting number: 21 64 32 16 8 4 2 1 It took 7 steps to complete the sequence
Once you’ve successfully completed the sequence be sure to fill out this Google form.
 Extension: Visualizing the Sequence
The sequences you formed above are known as hailstone sequences, because the terms move up and down but ultimately reach 1 like hailstones gaining layers of ice in a cloud.
This pattern can lead to some interesting visualizations of hailstone sequences.💻 Use your hailstone sequence code to create a visualization for the hailstone sequence using Turtle.
You can visualize the terms in a sequence starting with a specific number:
Or you can visualize the number of steps it takes to reach one from a set of integers:
💻 Add a random color element to the visualization
You will need to reference the following: