Squigss
Squigss

Reputation: 13

Summation of results from a loop

I would like to be able to calculate the sum of the 'totalTimes' that is returned by the loop. Any ideas on how to do it? Here is the code I currently have:

Thanks

subjectsNum = int(input ("How many subjects do you have to study?"))

def subject():
    for i in range (1, subjectsNum+1):

        subjectName = input ("What is the subject name?")
        pagesQuantity = float(input ("How many pages do you have to study?"))
        pagesTime = float (input ("How long do you reckon it will take to study one page (in minutes)?"))
        totalTime = (pagesQuantity) * (pagesTime)/60
        print("The estimated time to study %s is %s hours" %(subjectName, totalTime) )


subject()

Upvotes: 0

Views: 1348

Answers (3)

Aaditya Ura
Aaditya Ura

Reputation: 12669

If you want to use the return value of for loop you have to save the result somewhere and at last just sum the all result.

subjectsNum = int(input ("How many subjects do you have to study?"))

Final_total_time=[]

def subject():
    for i in range (1, subjectsNum+1):

        subjectName = input ("What is the subject name?")
        pagesQuantity = float(input ("How many pages do you have to study?"))
        pagesTime = float (input ("How long do you reckon it will take to study one page (in minutes)?"))
        totalTime = (pagesQuantity) * (pagesTime)/60
        print("The estimated time to study %s is %s hours" %(subjectName, totalTime) )
        Final_total_time.append(totalTime)


subject()

print(sum(Final_total_time))

output:

How many subjects do you have to study?2
What is the subject name?Physics
How many pages do you have to study?10
How long do you reckon it will take to study one page (in minutes)?2
The estimated time to study Physics is 0.3333333333333333 hours
What is the subject name?Python
How many pages do you have to study?5
How long do you reckon it will take to study one page (in minutes)?1
The estimated time to study Python is 0.08333333333333333 hours
0.41666666666666663

Upvotes: 0

Arthur Tacca
Arthur Tacca

Reputation: 9978

Have an extra variable, which you set to zero before, and add to it in the loop.

def subject(subjectsNum):
    totalSum = 0
    for i in range (subjectsNum):

        subjectName = input ("What is the subject name?")
        pagesQuantity = float(input ("How many pages do you have to study?"))
        pagesTime = float (input ("How long do you reckon it will take to study one page (in minutes)?"))
        totalTime = (pagesQuantity) * (pagesTime)/60
        print("The estimated time to study {} is {} hours".format(subjectName, totalTime) )
        totalSum += totalTime
    return totalSum


subjectsNum = int(input ("How many subjects do you have to study?"))
totalSum = subject(subjectsNum)
print("Sum is {}".format(totalSum))

By the way, I also made subjectsNum a parameter to subject(), and used the new-style format function, and looped i over [0,n-1] instead of [1,n].

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 54163

Sure, just have an accumulator list outside the loop.

def subject():
    times = []  # this is our accumulator
    for i in range(1, subjectsNum+1):
        ...
        times.append(totalTime)

    return times  # return it to the outer scope

times = subject()  # assign the return value to a variable
grand_total = sum(times)  # then add it up.

Upvotes: 1

Related Questions