How do I track Python code from start to finish? That shows the entire flow of execution, from which function is called first, which operations are performed, to the end of the entire flow.
Look at this example code, it receives an operation type (addition or subtraction) and two values (x and y), executes these two values according to the operation and at the end displays a message:
def calc(op, x, y):
if op == 'sum':
return x + y
elif op == 'subtraction':
return x - y
def msg(op, x, y):
if op == 'sum':
result = calc(op, x, y)
return "The result of the sum is: " + str(result)
elif op == 'subtraction':
result = calc(op, x, y)
return "The result of the subtraction is: " + str(result)
if __name__ == '__main__':
my_sum = msg('sum', 3, 2)
print(my_sum)
So this "tracking from start to finish" would look something like this:
- Line 17:
if __name__ == '__main__':
- Line 18:
my_sum = msg('sum', 3, 2)
- Line 8:
def msg(op, x, y):
- Line 9:
if op == 'sum':
- Line 10:
result = calc(op, x, y)
- Line 1:
def calc(op, x, y):
- Line 2:
if op == 'sum':
- Line 3:
return x + y
- Line 11:
return "The result of the sum is:" + str(result)
- Line 19:
print(my_sum)
And at the end it returns the message "The result of the sum is: 5".