0

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()
3
Contributors
3
Replies
23
Views
1 Year
Discussion Span
Last Post by tinstaafl
1
  • 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

Edited by slate: code tag does not work. wtf?

Votes + Comments
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
0

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

1

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)))
Votes + Comments
Why didn't it worked for me is a mistery
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.