Mason Golla
Mason Golla

Reputation: 17

How to i add the return values of a loop

I'm new to python a and I need to add the return values of a loop. the program is supposed to open a file containing dimensions in the format 1x31x6 sort them and do some math. I'm fairly certain I have everything right but I cannot figure out how to add the return values together. here is the code I have so far.

def parseAndSort(string):
    """this function reads the input file, parses the values, and sorts the ints from smallest to largest"""
    int_list = string.split('x')
    nums = [int(x) for x in int_list]
    nums.sort()
    length = nums[0]
    width = nums[1]
    height = nums[2]
    surface_area = (2 * length * width) + (2 * length * height) + (2 * width * height) + (length * width)
    tape = (2 * length) + (2 * width) + (length * width * height)
    return surface_area


def main():
    file = input('Please Input Measurement File :')
    try:
        output = open(file, "r")
    except FileNotFoundError:
        print("Error, please input a dimension file.")
    else:
        for ref in output:
            parseAndSort(ref)
        output.close()


if __name__ == "__main__":
    """ This is executed when run from the command line """
    main()

Upvotes: 1

Views: 41

Answers (1)

swaraj.renghe
swaraj.renghe

Reputation: 61

I'm assuming you mean you would like the sum of the return values of all the times you ran the function. You can keep a running sum, and keep adding each return value to it.

def main():
    sum = 0
    file = input('Please Input Measurement File :')
    try:
        output = open(file, "r")
    except FileNotFoundError:
        print("Error, please input a dimension file.")
    else:
        for ref in output:
            sum += parseAndSort(ref)
        output.close()
        print (sum)

Upvotes: 2

Related Questions