Count the Iterations
For vs. While
For Loops with range()
For Loops without range()
While Loops
100

How many times will the following loop iterate?

for i in range(7):

     print(i)

7
100

Suppose you are trying to get a user to enter multiple pieces of data until they enter "Quit".

Would this be better suited for a for loop or while loop?

While loop

100

Write a for loop that prints "Hi" five times.

for i in range(5):

     print("Hi")

100

How many times will the following loop iterate?

for c in "apple":

     print(c)

5

100

While loops terminate when their boolean condition evaluates to __________.

False

200

How many times will the following code print "Hi"?

for i in range(1, 10):

     for j in range(4, 8):

          print("Hi")

36

200

When is it better to use a for loop over a while loop?

When there is a known number of iterations.

200

Write a for loop that prints all multiples of 5 between 1 and 100.

for i in range(5, 101, 5):

     print(i)

200

Explain what the following code is doing:

word = input("Enter a word: ")

count = 0

for letter in word:

     if letter == "y":

          count += 1

print(count)

Prints how many y's are in a user-inputted word.

200

If a loop were created that started with

while True:

What keyword would be required  in the body of the loop in order to terminate the loop?

break

300

How many times will the following loop iterate?

for i in range(2, 18, 4):

     print(i)

4
300

Which type of loop has a risk of being an infinite loop?

while loop

300

Write a for loop that prints all numbers between 1 and 10 on the same line with a space in between.

for i in range(1, 11):

     print(i, end=" ")

300

What is wrong with the following code? Why?

for i in range("banana"):

     print(i)


range() is only used when iterating number to number.

300

What is a reasonable prompt to put in the blank?

ans = int(input("What is 2 + 2? "))

while ans != 4:

     ans = int(input("________________"))

Wrong! What is 2+2? 

400

How many times will the following loop iterate?

import random

i = random.randint(1, 5)

while i != 0:

   i = random.randint(1, 5)

Infinite

400

Rewrite the following code using a for loop instead of a while loop.

i = 0

while i < 4:

     print(i)

     i += 1

for i in range(4):

     print(i)

400

What would the following code print:

for i in range(3):

     for j in range(2):

          print(j)

0

1

0

1

0

1

400

What will the following code print?

for c in "Mississippi":

     if c != "i":

          print(c, end="")

Msssspp

400

What is wrong with the following code?

num = int(input("Enter a positive number: "))

while num <= 0:

     print("Try again!")

It could cause an infinite loop.

500

How many lines will the following code print?

for i in range(3):

     for j in range(10):

          print(i, j)

          if j > 3 and j % 3 == 0:

               break

18

500

Rewrite the following code as a while loop.

for i in range(1000000):

     ans = input("Enter your answer: ")

     if ans == "A":

          break

     else:

          print("Wrong.")

ans = input("Enter your answer: ")

while ans != "A":

     ans = input("Wrong. Enter your answer: ")

500

Write a nested for loop that prints every combination of numbers produced by two six-sided dice EXCEPT doubles. For example:

1 2

1 3

1 4

etc...

for i in range(1, 7):

     for j in range(1, 7):

          if i != j:

               print(i, j)

500

Finish this program that prints "Yes" if the user-inputted word has 6 letters.

word = input("Enter a word")

letters = 0

for c in word:

     letters += 1

if letters == 6:

     print("Yes")

500

Write a while loop that continuously prints random numbers between 1 and 10 until a 4 is randomly chosen. After 4 is printed, then the loop should terminate.

import random

num = 0

while num != 4:

     num = random.randint(1, 10)

     print(num)