POGIL FOR Loops
POGIL FOR Loops
POGIL FOR Loops
Learning Objectives
Students will be able to:
Content:
● Explain the difference between while loop and a F
OR loop
● Explain the syntax of a FOR loop
● Explain how to use the range() function in a F
OR loop
● Explain an accumulator in a FOR loop
Process:
● Write code that includes FOR loop
● Write code that uses use FOR loops within functions
b. Both programs produce the same output. Which code fragment is more concise?
c. for x in range(3,20,2):
print(x, end=" ")
3 5 7 9 11 13 15 17 19
d. numIterations = 6
for x in range(numIterations):
print(x, end=" ")
0 1 2 3 4 5
e. numIterations = 6
for x in range(1, numIterations+1):
print(x, end=" ")
0 1 2 3 4 5 6
3. After examining the five code fragments in #2, explain how the range() function works. Include an
explanation of the arguments (inputs).
The range function produces a sequence of numbers over time and produces an output based on that
sequence of numbers. Arguments in the range function change the range, the rate at which the range changes
and how much should be added to the range.
for x in range(3,19,3):
print(x, end=" ")
b. Rewrite this code using a list of values:
for x in range(1,5):
print(x, end=" ")
c. Why would you use the range() function when you could just list the numbers?
To be more efficient.
5. We learned about three actions that must occur in every loop:
x=1
while x<5:
print(x, end=" ")
x=x+1
b) How are these three actions implemented in the FOR loop? This is a bit tricky. Do the best you can
to explain.
The x in range(1,5) is the equivalent to the while line and the print is repeated five times.
Looping Structures: FOR Loops
b. Enter and execute the code to determine if you were correct. What does the program
actually do? Provide a detailed explanation.
The program prints a number from 1-4 with a period at the end (1.) and then it prints the inputted ice
cream flavor and makes an indent space.
8. Complete the arguments in the following range function so that the code prints: 5 4 3 2 1 0.
for x in range(5,-1,-1):
print(x, end=’ ‘)
Looping Structures: FOR Loops
10. Is it better to use a FOR loop when you know the number of times the loop should be executed or when
you do not know (support your answer with an explanation)?
It is better to use a FOR loop when you do know the number of times the loop should be
executed because it is more efficient than using a WHILE loop that would need a counter and it would
take up more space.
11. Determine if a WHILE loop or a FOR loop is more appropriate for the following
problems. Write the FOR or WHILE loop code snippet
a. Prompt the user for 6 numbers. Calculate the average of the six numbers and print it
to the screen.
Looping Structures: FOR Loops
total = 0
for x in range(6):
total=total+number
b. Prompt the user to enter numbers until the user decides he/she is finished. Calculate the
average of the numbers and print it to the screen.
number=int(input("Enter a number: "))
total=number
counter=1
doAgain="y"
while doAgain=="y":
total=total+number
counter=counter+1
c. Prompt the user for the amount of numbers she/he wants to enter. Prompt the user to enter
those values then find the average and print it to the screen.
amount=int(input("How many numbers do you want to enter: "))
x=0
total=0
while x<amount:
num=int(input("Please enter a number: "))
total=total+num
x=x+1
print("The average is:",total/amount)
Looping Structures: FOR Loops
1. Write a code segment using a FOR loop that prints multiples of 5 from 5 to 500, each one on its
own line.
for x in range(5,501,5):
print(x)
99 bottles of milk on the wall, 99 bottle of milk! Take 1 down, pass it around, 98 bottle
of milk on the wall.
98 bottles of milk on the wall, 98 bottles of milk! Take 1 down, pass it around, 97
bottles of milk on the wall.
1 bottles of milk on the wall, 1 bottles of milk! Take 1 down, pass it around, 0 bottles
of milk on the wall.
for x in range(99,0,-1):
print(x,"bottles of milk on the wall,",x,"bottle of milk! Take 1 down, pass it
around,",x-1,"bottle of milk on the wall.")