0

# 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:

3
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by bumsfeld
0

# 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?

0

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()
This question has already been answered. 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.