I can get the answer of cubes but I don't really know where to go from here to get the function to return back the answer and keep adding it to a sum! What do I have to do to make it keep adding to get a total?
def sumNCubes(n):
for i in range(n)
return (n-1)**3
def main():
number = int(raw_input("What number do you want to find the sum of cubes for?"))
n = number + 1
for i in range(number):
n = n - 1
print "The result of the sums is:", sumNCubes(n)
main()
You could simply do something like this:
def sumNCubes(n):
return sum(i**3 for i in range(1,n+1))
which uses a list comprehension to cube number in a range from 1-n+1 (1-n will not include n) then uses python built in sum
function to sum all of the cubes.
you could then just pass in your input and print it:
def main():
number = int(raw_input("What number do you want to find the sum of cubes for?"))
#this doesn't do anything but change n to 0
#for i in range(number):
# n = n - 1
print "The result of the sums is:", sumNCubes(number)
main()
with input of, for example, 5
, this will return:
>>> sumNCubes(5)
225