2

Hi totally new and struggling with a likely simple prob.
I have a for-loop creating values and printing the running total as they're generated. I need to find a way to print the sum of those running totals (in the case below: 3 + 33 + 333) I have a feeling a nested loop is an option but not sure where it'd go. Am I close? (Thanks)

Here is my code thus far:

def nSum(num):
    #calculating values for the variable (num) to the p power:
    p = 0
    newValue = 0
    for i in range(num):
        currentVal = num * 10**p
        p = p+1
    #running total of the curent values:
        newValue = currentVal + newValue
        print(newValue)
    return newValue

print(nSum(3)) #Want nSum to be total of all the newValues from the loop

2 Answers 2

3

If you want to keep track of the intermediate values:

def nSum(num):
    #calculating values for the variable (num) to the p power:
    values = []
    for p in range(num):
        currentVal = num * 10**p
        #running total of the curent values:
        newValue = currentVal + (values[-1] if values else 0)
        values.append(newValue)

    return values

print(sum(nSum(3))) #Want nSum to be total of all the newValues from the loop

If you don't care about them, you can drop the list and just use an accumulator:

def nSum(num):
    #calculating values for the variable (num) to the p power:
    total = 0
    new_value = 0
    for p in range(num):
        currentVal = num * 10**p
        #running total of the curent values:
        new_value += currentVal
        total += new_value

    return total

print(nSum(3))

P.S. You don't need to define and increment p -- you can just use your (currently unused) variable i -- which is incremented automatically by range.

Sign up to request clarification or add additional context in comments.

Comments

-2

Do you want this?

def nSum(num):
    #calculating values for the variable (num) to the p power:
    p = 0
    newValue = 0 
    total_value=0 #new variable
    for i in range(num):
        currentVal = num * 10**p
        p = p+1
    #running total of the curent values:
        newValue = currentVal + newValue
        print(newValue)
        total_value+=newValue #new line
    return total_value

print(nSum(3)) #Want nSum to be total of all the newValues from the loop

Or you can do it without using p variable.You can do it only using i variable.Like :

def nSum(num):
    #calculating values for the variable (num) to the p power:
    newValue = 0
    total_value=0
    for i in range(num):
        currentVal = num * 10**i
    #running total of the curent values:
        newValue = currentVal + newValue
        print(newValue)
        total_value+=newValue
    return total_value

print(nSum(3)) #Want nSum to be total of all the newValues from the loop

5 Comments

huh? Why are you using p = 0, p += 1? Just use i.
I know this, but i tried to make my answer as same as question. It will be more helpful for him to get my answer.
@FHTMitchell I have updated my answer as you suggested. Thanks
Did you really edit your answer to mirror mine? What's the point?
We have answered almost at the same time. And then I have updated my answer as FHTMitchell suggested. That's it. The idea was same, may be that's why there is similarity between our answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.