How many times will the following loop iterate?
for i in range(7):
print(i)
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
Write a for loop that prints "Hi" five times.
for i in range(5):
print("Hi")
How many times will the following loop iterate?
for c in "apple":
print(c)
5
While loops terminate when their boolean condition evaluates to __________.
False
How many times will the following code print "Hi"?
for i in range(1, 10):
for j in range(4, 8):
print("Hi")
36
When is it better to use a for loop over a while loop?
When there is a known number of iterations.
Write a for loop that prints all multiples of 5 between 1 and 100.
for i in range(5, 101, 5):
print(i)
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.
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
How many times will the following loop iterate?
for i in range(2, 18, 4):
print(i)
Which type of loop has a risk of being an infinite loop?
while loop
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=" ")
What is wrong with the following code? Why?
for i in range("banana"):
print(i)
range() is only used when iterating number to number.
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?
How many times will the following loop iterate?
import random
i = random.randint(1, 5)
while i != 0:
i = random.randint(1, 5)
Infinite
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)
What would the following code print:
for i in range(3):
for j in range(2):
print(j)
0
1
0
1
0
1
What will the following code print?
for c in "Mississippi":
if c != "i":
print(c, end="")
Msssspp
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.
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
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: ")
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)
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")
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)