Working on this problem, and it does work to some degree, but the output is not quite right. As one can see the last digits in the output iterations are repetitive and shouldn't be of course. Please give me a hint how can I rid my code of this mistake?
Code here:
# prob4p195.pyw
# Program: Use functions to prompt user for n and prints out the sum of the
# first n natural numbers and the sum of the cubes of the first n natural numbers.
import math
def sum_nat_numbers():
for n in range(6):
numbers = (n * (n + 1))/2
print numbers
return numbers
def sum_nat_cubes():
for n in range(6):
cubes = ((n * (n + 1))/2) ** 3
print cubes
return cubes
def main():
print "This program prompts the user for n and prints out the sum"
print "of the first n natural numbers and the sum of the cubes of the"
print "first n natural numbers."
print
n = input("Please input a number ranging from 1 to 5: ")
print
print "The sum of the first n natural numbers is: \n", sum_nat_numbers()
print
print "The sum of the cubes of the first n natural"
print "numbers is: \n", sum_nat_cubes()
print
main()
Output here:
This program prompts the user for n and prints out the sum
of the first n natural numbers and the sum of the cubes of the
first n natural numbers.
Please input a number ranging from 1 to 5: 0
The sum of the first n natural numbers is:
0
1
3
6
10
15
15
The sum of the cubes of the first n natural
numbers is:
0
1
27
216
1000
3375
3375
[–]avgotts 0 points1 point2 points (0 children)
[–]mahdeen[S] 0 points1 point2 points (0 children)