number = str(input('Please input a number: '))
def add_numbers(number):
sum = 0
for i in range (1,4):
number = number * i
sum = int(number)
return sum
print(add_numbers(number))
Output for range (1,2) in the loop = 5
Output for range(1,3) in the loop = 60 Output for range (1,4) in the loop = 555615
I tried it for cases where the loop iterates only once or twice which results in 5, and 5 55 = 60, respectively. But when I run it for 3 times it returns 555615. I'm guessing that it returns the correct sum which is 615 but is then added with the string 555 (which I don't understand why this does happen.
CodePudding user response:
This should work:
number = str(input('Please input a number: '))
def add_numbers(number):
number = int(number)
sum = 0
for i in range(1,4):
number = number ** i
sum = int(number)
return sum
print(add_numbers(number))
Output:
Please input a number: 5
15655
Instead of number * i
you need number
to the power of i
or number ** i
. Also, since you are passing add_numbers
a value from input()
, number
is a string and you need it to be an int
which number = int(number)
does.
CodePudding user response:
sum will be like that = number *1 number *2 number *3 ... and u want = number number * number number * number * number so u should do that : sum = pow(number,i)