Lab: Loops

3 minute read

Preparation

These exercises should be completed with your assigned partner. Both of you will take turns playing the role of the driver (the one currently screen sharing) and the navigator. Please refer to the course Code of Conduct for more details.

Before you begin, you determine who will serve as the driver. One good way to decide is to choose whose name comes alphabetically first. On Tuesday, you’ll switch roles.

Exercise 1

Consider the following loop.

count = 0
for i in range(10):
    if i % 3 == 0:
        count = count + 1
    
    # print("Currently, i =", i, "and count =", count)

print("The final count is:", count)

a. Predict what the value of count is when it is printed.

b. Verify your prediction by copying and pasting the code into Thonny and running it.

c. Uncomment the print statement and rerun the code to see the intermediate steps of the loop.

d. What does the final count represent? Discuss with your partner.

e. If you’re curious, run the code in debug mod in Thonny to watch it execute step by step.

Exercise 2

Consider the following program.

def main():
    num = int(input("Enter an int divisible by two: "))

    while num % 2 == 1:
        print("That wasn't an even number!")
        num = int(input("Enter an int divisible by two: "))

    print("You entered", num)

main()

a. Copy and paste the code into a new file called loop_input.py.

b. Run the code and try entering in various odd and even integers. Discuss the code with your partner and make sure you understand what it is doing.

c. Modify the program so that instead of requiring the user to type in an even number, they must type in a number between 0 and 100.

Hint: The main thing you need to change is the condition of the while loop.

Exercise 3

One example from the reading was the following loop:

while True:
    line = input('> ')
    if line == 'done':
        break
    print(line)

print('Done!')

a. Copy and paste the code into a new file called break_example.py.

b. Run the code and try entering various things. Finally type “done” to see that the program finally stops. Discuss the code with your partner and make sure you understand what it is doing.

c. Rewrite the code so that it no longer uses a break statement.

Hint: You will need to replace the True part of while True with a condition that looks similar to line == "done" of the conditional.

Exercise 4

Below is a function that prints the first n powers of two.

def powers_of_two(n):
    cur_power = 1
    for i in range(n):
        print(cur_power)
        cur_power = 2 * cur_power

a. Copy and paste the code into a new file called powers_of_two.py.

b. Run the code so that you have access to the function in the shell and try running it on several arguments of n. Discuss the code with your partner and make sure you understand what it is doing.

c. Rewrite the function so that it uses a while loop instead of a for loop.

Hint: Consider decrementing the value of n every time the loop executes and then having the loop stop when n reaches zero.

Exercise 5

Write a program named average.py that does the following:

  1. Prompts the user with a message saying Enter as many numbers as you'd like and then type "done".
  2. Prints a > symbol letting the user know they are being asked to type something.
  3. When the user types "done", your program should print the average of all the numbers they typed in. For example, when you run the program you might see:
Enter as many numbers as you'd like and then type "done".
> 7 
> 3
> 5
> done
The average is: 5.0