Skip to main content

Section 5.8 Evaluate Loops-WE4-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.8.1 Evaluate Loops-WE4-P1

Exercises Exercises

1.
    Q16: What is the output of the following loop if the user input is: 10, 20, 30, 35, 40, -1
    print("Enter a negative score to signal the end of the input.")
    total = 0
    count = 0
    score = int(input("Score: "))
    while score >= 0:
        count += 1
        total += score
        score = int(input("Score: "))
    print(total/count)
    
  • 27.0
  • 27
  • 22.5
  • 22
  • Runtime error
2.
    Q17: What is the output of the following loop if the user input is: 10, 20, 30, 35, 40, -1?
    print("Enter a negative score to signal the end of the input.")
    total = 0
    count = 0
    score = int(input("Score: "))
    while score >= 0:
        score = int(input("Score: "))
        count += 1
        total += score
    print(total/count)
    
  • 27.0
  • 22.5
  • 24.8
  • 20.666667
  • Runtime error
3.
    Q18: What is the output of the following loop if the user input is: 10, 20, 30, 35, 40, -1
    print("Enter a negative score to signal the end of the input.")
    total = 0
    count = 0
    score = int(input("Score: "))
    while score >= 0:
        total += score
        score = int(input("Score: "))
        count += 1
    print(total/count)
    
  • 27.0
  • 27
  • 22.5
  • 22
  • Runtime error
4.
    Q19: What is the output of the following loop if the user input is: 20, 60, 75, 35, 95, -1
    print("Enter a negative score to signal the end of the input.")
    passing = 0
    failing = 0
    score = int(input(“Score: “))
    while score >= 0:
        if score >= 60:
            passing += 1
        else:
            failing += 1
        score = int(input(“Score: ”))
    print("Percent passing: " + (passing / (passing + failing) * 100))
    
  • Percent passing: 60
  • Percent passing: 75
  • Percent passing: 80
  • Percent passing: 85
  • Percent passing: 90
5.
    Q20: What is the output of the following loop if the user input is: 1, 3, 9, 6, 9, 2, -1
    print(“Enter a negative value to signal the end of the input.”)
    val = int(input(“Value: “))
    max = value
    min = value
    val = int(input(“Value: “))
    while val >= 0:
        if val > max:
            max = val
        if val < min:
            min = val
        val = int(input(“Value: “))
    print(“Maximum value was “ + max + “ and minimum value was “ + min)
    
  • Maximum value was 9 and minimum value was 1
  • Maximum value was 6 and minimum value was 1
  • Maximum value was 9 and minimum value was -1
  • Maximum value was 3 and minimum value was 1
  • Maximum value was 9 and minimum value was 2
You have attempted of activities on this page.