ΣνΜDr. Sharon's Math PlatformSyvara Learning
Question Bank › 📘 Undergraduate › Programming for Engineers › Q11

Q11 · Python: debug a loop summing a list with print tracing

Error Handling and Debugging 🟢 Intro Coding
Question

The code below should compute the sum of [3, 7, 2, 9, 5] but returns the wrong answer:

  numbers = [3, 7, 2, 9, 5]
  total = 1
  for num in numbers:
      total += num
  print("Total:", total)

(a) Add print statements inside the loop to trace num and total after each step.
(b) Identify the bug.
(c) Fix the code and verify the correct answer is 26.

🔒

Log in to see the worked solution

Enter your email to get started. Your first free solutions in every course unlock as soon as you verify.

Log in or sign up with email
← Previous Next →
Working…