Skip to main content

Section 5.10 Evaluate Loops-WE5-P1

Subgoals for Evaluating a Loop.

  1. Determine Loop Components
    1. Start condition and values
    2. Iteration variable and/or update condition
    3. Termination/final condition
    4. Body that is repeated based on indentation
  2. Trace the loop, writing updated values for every iteration or until you identify the pattern

Subsection 5.10.1 Evaluate Loops-WE5-P1

All of the following problems use the same setup, but they are independent of each other.
high_numbers = [300, 200, 100, 400, 500, 300]
low_numbers = [1, 3, 2, 4, 5, 3]
mixed_numbers = [5, 3, -4, 7, 2]

Exercises Exercises

1.
    Q21: What is the output of the following loop?
    total = 0
    for number in high_numbers:
        total = total + number // 100
    print(total)
    
  • 3
  • 6
  • 18
  • 18.0
  • 300
  • 1800
2.
    Q22: What is the output of the following loop?
    total = 0
    for number in high_numbers:
        total = total + 100
    print(total)
    
  • 6
  • 18
  • 600
  • 1800
  • 1900
  • 2400
3.
    Q23: What is the output of the following loop?
    combined = ""
    for number in low_numbers:
        combined = combined + str(number)
    print(combined)
    
  • 18
  • "18"
  • "132453"
  • 6
  • "354231"
4.
    Q24: What is the output of the following loop?
    max = 0
    for number in low_numbers:
        if number > max:
            max = number
    print(max)
    
  • 5
  • 4
  • 3
  • 2
  • 1
5.
    Q25: What is the output of the following loop?
    sum = 0
    for number in high_numbers:
        sum += number
    print(sum / len(high_numbers))
    
  • 150
  • 400
  • 250
  • 360
  • 300
You have attempted of activities on this page.