While loops #
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.
[0] Set up #
๐ Github Repo: github.com/Making-with-Code/lab_while
๐ป Now that you have the lab, go into its folder.cd lab_while
๐ป 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.
poetry shell
๐พ ๐ฌ Exiting the poetry shell
When you want to exit the shell, you can type
exitor^D
๐ป Take a look at the files inside with:
 ls
- guessing_game.py
- hailstone_sequence.py
[1] What is a While Loop? #
Conditions #
while loops use conditions just like if statements. You can use operators to compare
values or to generate True or 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 True like
this: 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...")
[2] 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.
python guessing_game.py
----------------------------
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 Visual Studio Code
When you want to stop running a program, type
^Cin the terminal.
code guessing_game.py
 
 
๐พ 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
[3] Hailstone Sequence #
Now that you’ve gotten practice with while loops, you will be exploring a special sequence known as the
hailstone sequence.
This sequence results from the following rules (known as the Collatz conjecture):
- take any positive number n
- find the next term of the sequence using the following rules:
- if nis even, the next term isn/2
- if nis odd, the next term isn*3+1
 
- if 
- 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 1
๐ค 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 hailstone_sequence.py.
code hailstone_sequence.py
โ๏ธ 
 Use comments to complete the pseudocode to find the hailstone sequence of any starting number. There are currently three line of pseudocode in the file.
A comment is any line that starts with a
#
# this is a commentYou can comment code quickly by highlightly multiple lines and clicking
โ+?- this will comment or uncomment code.
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
[4] Deliverables #
โกโจ
Once you’ve successfully completed the sequence be sure to fill out this Google form.
โ๏ธ Add a screenshot of a code snippet to your
CS9 Lab Code Login your Google Drive. Add a comment either asking a question about your code OR describing a piece of code you are proud of.
[5] 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.
 
 Hailstones forming 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:
 
 Terms in hailstone sequence starting at 590
        
Or you can visualize the number of steps it takes to reach one from a set of integers:
 
 Steps to reach one in hailstone sequence as radii of half circles for integers 1-100
        
๐ป Add a random color element to the visualization
You will need to reference the following:
