CSC111 Debugging a Loop

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 08:28, 25 September 2015 (EDT)


Debugging a Loop


# averageAge.py
# D. Thiebaut
#
# Example of how one would go about debugging a simple
# program and reveal how the loop works.
#
def main():

    # initialize variables
    sum = 0
    count = 0
    print( "sum = {0:1} count = {1:1}".format( sum, count ) )
    input()

    # loop through a list of ages and compute the total sum
    # of the ages, and the number of values in the list.
    for age in [20, 19, 21, 20, 21, 29, 17]:
        sum = sum + age
        count = count + 1
        print( "age = {0:5} sum = {1:5} count = {2:5}".format( age, sum, count ) )
        input()

    # compute the average, displays quantities of interest
    print( "-" * 30 )
    print( "sum = ", sum )
    print( "count = ", count )
    print( "average = ", sum/count )

main()