Skip to main content

Section 7.3 Assessment: Writing Loops

Subgoals for Writing a Loop.

  1. Determine purpose of loop
    1. Pick a loop structure (while, for, do_while)
  2. Define and initialize variables
  3. Determine termination condition
    1. Invert termination condition to continuation condition
  4. Write the loop body
    1. Update Loop Control Variable to reach termination

Exercises Exercises

    2.

    Q2: Given the following code segment which is intended to print the number of integers that evenly divide the integer input_val. (You may assume that input_val > 0.)
    count = 0
    input_val = int(input())
    for k in range(1, input_val):
        if ___:
            count += 1;
    print(count)
    
    Which of the following can be used to replace ___ so that the code will work as intended?

    5.

    Q5: Fill in the blanks in the following code to create a program that will print the prime numbers between 1 and 100 (inclusive).
    for i in range(__A__, __B__):
        is_prime = True
        for j in range(__C__, __D__):
            if i % j == __E__:
                is_prime = False
        if is_prime:
            print(i, end=" ")
    
    Blank A:
    Blank B:
    Blank C:
    Blank D:
    Blank E:
You have attempted of activities on this page.