I need to comput and display the sum and average of the cubes of the even natural numbers between 2 and n. Where n is entered by the user. Im stuck on how to average the sum of the cubes. Help Please. This is what I have so far.

def main():
print ("This calculates the sum of the cubes of the 1st natural numbers")
natn = eval(input("How many cube numbers do you want to sum? "))
add = 0
for i in range (natn):
    add = add + i**3
print (add)
main()

Recommended Answers

All 3 Replies

  • You are not adding up the even cubes. You are adding up all cubes of numbers between 0 and natn-1.Look up range documentation, how to generate even numbers.
  • Average is sum divided by count. You can count the numbers by (python3):

    add = 0
    counter=0
    for i in range (natn):
    add = add + i**3
    counter = counter +1
    average=add/counter

  • The whole problem can be solved in the end by:

Cannot copy paste code, because code tag does not work here on daniweb...
Pastebin:http://pastebin.com/sYUJh7Qq

commented: FYI - you can copy and paste code. To get it to format properly select all code after you've pasted it and hit tab.Make empty line before & after +13

Thanks for your help. I'm in the right direction now.

Here's the code from the pastebin:

print ("This calculates the sum of the cubes of the 1st natural numbers")
natn = eval(input("How many cube numbers do you want to sum? "))
print("sum of even cubes between 0 and {} : ".format(natn-1),sum(i**3 for i in range(0,natn,2)),"\n",
      "sum of cubes between  0 and {}".format(natn-1),sum(i**3 for i in range(natn)),"\n",
       "average of cubees between 0 and {}".format(natn-1),sum(i**3 for i in range(natn))/sum(1 for i in range(natn)))
commented: Why didn't it worked for me is a mistery +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.