Home > Net >  trying to sum numbers after finding their exponents using for loop
trying to sum numbers after finding their exponents using for loop

Time:11-25

I am trying to find what is the sum of numbers after finding their exponents I am trying to print something like this:
A exponent 1 A exponent 2 A exponent 3 ... A exponent n

finding exponents but can't sum them

here is what I have done so far:

number = int(input('Enter number: '))
exponent = int(input('Enter grade of the number: '))

for i in range(1, exponent   1):
    # print(i)
    numbers = number ** i
    # numbers = numbers   numbers
    print(numbers)

now I am trying to find sum of all exponent numbers, what am I missing?

CodePudding user response:

Try this:

result = sum(number**i for i in range(1, exponent   1))

CodePudding user response:

You have to define a starting point (0) and to sum the value at each step:

number = int(input('Enter number: '))
exponent = int(input('Enter grade of the number: '))

numbers = 0
for i in range(1, exponent   1):
    numbers  = number ** i
print(numbers)

CodePudding user response:

I'm wondering if you are trying to print a string or the sum. I guess the latter. Then, I think you should define an 'accumulator': a variable that is initialized with value 0 and stores the sum. Like the following:

number = int(input('Enter number: '))
exponent = int(input('Enter grade of the number: '))

accum = 0
for i in range(1, exponent   1):
    accum  = number ** i
print(accum)


  • Related