Unit 1

While Loops

Code Reading and Code Tracing

[8.1]

What will be the output of the following Python code?

i = 0
while (i < 5):
    print("hi")
    i = i + 1

Choose the correct option:

  1. Hi
    Hi
    Hi
    Hi
    Hi

  2. hi
    hi
    hi
    hi
    hi

  3. Hi
    hi
    hi
    hi
    hi

  4. hi
    hi
    hi
    hi
    hi
    hi

[8.2]

What will be the output of the following Python code?

i = 0
while (i < 3):
    print("hi")
    i = i + 1
print("bye")

Choose the correct option:

  1. hi
    bye
    hi
    bye
    hi
    bye

  2. hi
    hi
    hi
    bye

  3. hi
    bye

[8.3]

What will be the output of the following Python code?

i = 0;
while (i < 0):
    print("hi")
  

Choose the correct option:

  1. It will output an error!

  2. It won't output anything.

  3. It will output this line: hi

[8.4]

What will be the output of the following Python code?

x = 3;
i = 0;
while i < 3:
  x += 1
  i += 1

print(x)

  

Choose the correct option:

  1. 6

  2. 7

  3. 3

  4. 4

[8.5]

What will be the output of the following Python code?

i = 3
while i < 6:
  print(i)
  i += 1
  

[8.6]

What will be the output of the following Python code?

i = 0;
while i < 3:
  print(i)
  i = i + 1
  

[8.7] In the code below, which is the loop control variable x or i? Why?

x = 5
i = 3
while i < 13:
    x = x + 2
    i = i + 2

[8.8] What will be the output of the following code? Circle the correct answer

count = 0
while count < 5:
    print("Count:", count)
    count += 1

print("Loop finished.")
  • Count: 0, Count: 1, Count: 2, Count: 3, Count: 4, Loop finished.
  • Count: 0, Count: 1, Count: 2, Count: 3, Count: 4, Loop finished.
  • Count: 0, Count: 1, Count: 2, Count: 3, Count: 4, Count: 5, Loop finished.

[8.9] Trace the code below. What will it print once the code is done executing?

num = 5
while num <= 19:
    print(num)
    num += 2

[8.10] Trace the code below. What will it print once the code is done executing?

num = 1
while num <= 49:
    if num % 7 == 0:
        print(num)
    num += 1

[8.11] Trace the code below. What will it print once the code is done executing?

num = 5
while num <= 16:
    if num % 2 == 0:
        print("even")
    else:
        print("odd")
    num += 1

[8.12] Trace the code below. What will it print once the code is done executing? What are the loop control variables?

n = 5
k = 5
i = 0
while i <= n:
    j = k - i
    while j > 0:
        print(j)
        j -= 1
    print()
    i += 1

[8.13] What will the value of 'total' be after the following code executes? What about the value of num?

num = 1
total = 0
while num <= 10:
    total += num
    num += 1

[8.14] What will be the value of 'result' after the following code executes?

num = 10
result = 1
while num > 0:
    result *= num
    num -= 1

[8.15] What will the value of 'product' be after the following code executes?

outer_num = 1
while outer_num <= 4:
    inner_num = 1

    while inner_num <= 4:
        product = outer_num * inner_num
        inner_num += 1

    outer_num += 1

[8.16] What will be the output of the following code?

outer = 1
while outer <= 3:
    inner = 1
    while inner <= outer:
        print(inner)
        inner += 1
    outer += 1

[8.17] Trace the code below. What will it print in each iteration? Leave the cell empty if nothing will be printed.


num = 12
count = 0

while num <= 19:
    if num % 3 == 0:
        count = count + 1
    elif num % 9 == 0:
        count = count + 1
    else:
        print("not multiple")
    num = num + 1

Code Writing

[8.18] Write a while loop that continuously prompts the user for input until they enter 'quit'. Print each input received.

[8.19] Write a while loop that prints the integers 2 through 10.

[8.20] Write a function print_integers_between(a, b) that prints the integers between a and b where a and b are given as parameters. Use a while loop.

[8.21] Write a while loop that will print every 7 numbers from 100 to 57.

[8.22] Write a function sum_numbers() that takes no parameters and that uses a while loop to add all the numbers from 15 to 35.

[8.23] Write a function calculate_sum() that takes input a number from a user and calculates the sum of all numbers from 1 to the given number. Use a while loop.

[8.24] Write a function count_multiples_of_3_and_5(n) that takes a number n as a parameter and counts how many multiples of 3 and 5 are between 1 and n. Use a while loop.

[8.25] Write a function check_multiples(n) that takes a number n as a parameter and prints for each number from 1 to n whether it is a multiple of 3, a multiple of 5 or both a multiple of 3 and 5. Use a while loop.

Here is an example of the output when we call the function with n = 20:


>>> check_multiples(20)
0 is a multiple of both 3 and 5.
1 is neither a multiple of 3 nor 5.
2 is neither a multiple of 3 nor 5.
3 is a multiple of 3.
4 is neither a multiple of 3 nor 5.
5 is a multiple of 5.
6 is a multiple of 3.
7 is neither a multiple of 3 nor 5.
8 is neither a multiple of 3 nor 5.
9 is a multiple of 3.
10 is a multiple of 5.
11 is neither a multiple of 3 nor 5.
12 is a multiple of 3.
13 is neither a multiple of 3 nor 5.
14 is neither a multiple of 3 nor 5.
15 is a multiple of both 3 and 5.
16 is neither a multiple of 3 nor 5.
17 is neither a multiple of 3 nor 5.
18 is a multiple of 3.
19 is neither a multiple of 3 nor 5.
20 is a multiple of 5.

[8.26] Write a method print_in_square_brackets(n) that will print the numbers 1 through n in square brackets.
For example, if n = 6, the function will print [1][2][3][4][5][6].
Use a while loop.

[8.27] Write a function count_digits(n) that will count the total number of digits in the given number n using a while loop. For example, if the number n is 75869, the oreturned utput should be 5 as there are 5 digits.

[8.28] Write a function to print the following pattern. Use a while loop.


****
****
****
****
    

[8.29] Write a function to print the following pattern. Use a while loop.


* * *
 * * *
* * *
 * * *
* * *

[8.30] Write a function to print the following pattern. Use a while loop.


*
**
***
****
*****

[8.31] Write a function to print the following pattern. Use a while loop.


x-x-x
-o-o
====
x-x-x
-o-o
====
x-x-x