Debugging and testing · 7 / 14 Exercise Easy

Tracing a loop

Objective

Create a Python program that uses traces (print messages) to follow the value of the variables step by step, the simplest debugging technique.

Instructions

  • Read a positive integer n and go through its digits from left to right (with str(n)) adding them up in the variable total.
  • In each turn show a trace with the format Digit D: total = S, where S is the already updated total.
  • When finished show Total sum: S.
Input
1234
Output
Digit 1: total = 1
Digit 2: total = 3
Digit 3: total = 6
Digit 4: total = 10
Total sum: 10

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise