Back to Student Worksheet
CS Grade 6-8 Answer Key

Python: Loops (for and while)

Practice tracing, writing, and choosing loops in Python

Answer Key
Name:
Date:
Score: / 15

Python: Loops (for and while)

Practice tracing, writing, and choosing loops in Python

CS - Grade 6-8

Instructions: Read each problem carefully. Trace the code step by step when needed. Write complete code or explanations in the space provided.
  1. 1

    What does this code print? for i in range(4): print(i)

    Remember that the stopping number in range is not included.

    The code prints 0, 1, 2, and 3 on separate lines. In Python, range(4) starts at 0 and stops before 4.
  2. 2

    Write a for loop that prints the numbers 1 through 5, one number per line.

    One correct answer is: for number in range(1, 6): print(number) This works because range(1, 6) includes 1, 2, 3, 4, and 5.
  3. 3

    What does this code print? count = 3 while count > 0: print(count) count = count - 1 print("Go!")

    Track how count changes after each time the loop runs.

    The code prints 3, 2, 1, and then Go! on separate lines. The while loop runs while count is greater than 0.
  4. 4

    Choose the better loop type for this task and explain why: Print each item in a list of favorite foods.

    A for loop is the better choice because the program needs to visit each item in a list. For loops are useful when looping through a sequence.
  5. 5

    Choose the better loop type for this task and explain why: Keep asking a user for a password until they type the correct password.

    Think about whether you know exactly how many times the loop should run.

    A while loop is the better choice because the number of guesses is not known ahead of time. The loop should continue while the password is incorrect.
  6. 6

    Fill in the blank so the loop prints 2, 4, 6, 8, and 10. for n in range(2, 11, ____): print(n)

    The blank should be 2. The code range(2, 11, 2) counts by 2 from 2 up to 10.
  7. 7

    Trace this code and write the final value of total. total = 0 for n in range(1, 4): total = total + n

    Make a small table with the values of n and total after each loop.

    The final value of total is 6. The loop adds 1, then 2, then 3, so total becomes 0 + 1 + 2 + 3.
  8. 8

    This code is supposed to print 1, 2, and 3, but it has a bug. Fix it. x = 1 while x <= 3: print(x)

    A while loop usually needs a line that changes the condition.

    The fixed code is: x = 1 while x <= 3: print(x) x = x + 1 The original code caused an infinite loop because x never changed.
  9. 9

    What does this code print? for letter in "cat": print(letter)

    The code prints c, a, and t on separate lines. A for loop can loop through each character in a string.
  10. 10

    Write a while loop that prints the numbers 5 down to 1, one number per line.

    Start with 5, print it, then subtract 1 each time through the loop.

    One correct answer is: number = 5 while number >= 1: print(number) number = number - 1 This works because the variable starts at 5 and decreases by 1 each time.
  11. 11

    Look at this code. for i in range(3): print("Hello") How many times does it print Hello?

    It prints Hello 3 times. The loop runs once for each value in range(3), which is 0, 1, and 2.
  12. 12

    Complete the code to print each animal in the list. animals = ["dog", "cat", "rabbit"] for animal in animals: __________

    Use the loop variable inside the print function.

    The blank should be print(animal). The completed loop prints each animal in the list on its own line.
  13. 13

    What is the value of x after this code runs? x = 10 while x < 20: x = x + 3

    Keep adding 3 until the condition x < 20 is false.

    The final value of x is 22. The values are 10, 13, 16, 19, and then 22, and the loop stops because 22 is not less than 20.
  14. 14

    Write a for loop that prints the square of each number from 1 through 4. The output should be 1, 4, 9, and 16 on separate lines.

    One correct answer is: for n in range(1, 5): print(n * n) This prints the square of each number from 1 through 4.
  15. 15

    Explain the difference between a for loop and a while loop in Python. Give one example of when each loop type is useful.

    Think about known repetition compared with repetition that depends on a condition.

    A for loop is useful when you know the sequence you want to loop through, such as numbers in a range or items in a list. A while loop is useful when you want to repeat until a condition changes, such as asking for input until the user gives a correct answer.
LivePhysics™.com CS - Grade 6-8 - Answer Key