# sum.py
# tpm
# A program that accepts an indeterminate (any number) of integers entered by
# the user, calculates and their sum, using loop

from math import *

def calc_sum():
    n = input("Enter any number: ")
    sum = 0.0
    for i in range(n):
        x = input("Enter a number >> ")
        sum = sum + x
    print "\nThe sum of the numbers is", sum / n

main()

:sad: :mad: :confused: :o :( :cry: :evil:

# sum.py
# tpm
# A program that accepts an indeterminate (any number) of integers entered by
# the user, calculates and their sum, using loop

from math import *

def calc_sum():
    n = input("Enter any number: ")
    sum = 0.0
    for i in range(n):
        x = input("Enter a number >> ")
        sum = sum + x
    print "\nThe sum of the numbers is", sum / n

main()

:sad: :mad: :confused: :o :( :cry: :evil:

why do you need to /n when you only want to calc the sum of these numbers?
and where is your main() ?? it should be calc_sum() right?

Use the while loop for indeterminate number of entries, for loop for fixed number. If you need to use main(), define main to call your calc_sum function. Let the user know how to stop the loop.

# while loop is used to sum an indeterminate number of integers entered, until you enter a zero 

def calc_sum():
    sum = 0
    x = input("Enter a number (0 to stop) >> ")
    while x != 0:
        sum = sum + x
        x = input("Enter a number (0 to stop) >> ")
    print "\nThe sum of the numbers is", sum 

def main():
    calc_sum()

main()
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.